Java 如何在终端上正确打印月历
您好,提前感谢您的帮助。我有一个程序,可以根据用户输入的月份和年份打印当前的月历。该计划主要是工作,但我有格式的问题,一个月的第一天不是在适当的日期开始 示例输出:Java 如何在终端上正确打印月历,java,date,calendar,formatter,leap-year,Java,Date,Calendar,Formatter,Leap Year,您好,提前感谢您的帮助。我有一个程序,可以根据用户输入的月份和年份打印当前的月历。该计划主要是工作,但我有格式的问题,一个月的第一天不是在适当的日期开始 示例输出: October 2020 ------------------------------ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
October 2020
------------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
2020年10月将于周四开始,但1
将在Mon
下打印。10月4日是周日,所以新线应该从这里开始
请参阅附件我的代码。再次感谢
import java.util.*;
public class CalendarMonthDisplay {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Scan for user input
System.out.print("Please enter a month between 1 and 12 (e.g. 5): "); //Prompt user to enter month
int m = input.nextInt();
System.out.print("Please enter a full year (e.g. 2018): "); //Prompt user to enter year
int y = input.nextInt();
//Print calendar for the month of the year
if ( m < 1 || m > 12)
System.out.print("Wrong input! Please try again.");
else
printMonthCalendar(m, y);
}
static void printMonthCalendar (int m, int y) { //Display calendar in format above
int startDay = getStartDay(m, y);
int numDaysInMonths = getNumDaysInMonth(m, y);
printMonthHeader(m, y);
printMonthBody(startDay, numDaysInMonths);
}
static void printMonthBody (int startDay, int numDaysInMonths) { //Display the days in the calendar
int i;
for (i = 0; i <= startDay; i++)
System.out.print(" ");
for (i = 1; i <= numDaysInMonths; i++) {
if ( i < 10 )
System.out.print(" " + i );
else
System.out.print(" " + i + " ");
if ((startDay + i) % 7 == 0)
System.out.println();
}
System.out.println();
}
static void printMonthHeader (int m, int y) { //Display the header information
System.out.println("\t" + getMonthName(m) + " " + y);
System.out.println("------------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
static String getMonthName (int m) {
String monthName = null;
switch (m) {
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "August";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
}
return monthName;
}
static int getNumDaysInMonth (int m, int y) {
int numDaysInMonths= 0;
switch (m) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
numDaysInMonths= 31;
break;
case 4: case 6: case 9: case 11:
numDaysInMonths = 30;
break;
case 2:
if (isLeapYear(y))
numDaysInMonths = 29;
else
numDaysInMonths = 28;
break;
}
return numDaysInMonths;
}
static boolean isLeapYear (int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
// return true;
// return false;
}
static int getStartDay (int m, int y) {
// Adjust month number & year to fit Zeller's numbering system
if (m < 3)
m = m + 12;
y = y - 1;
int d = 1; //Set day parameter to 1
int k = y % 100; // Calculate year within century
int j = y / 100; // Calculate century term
int h = 0; // Day number of first day in month 'm'
h = ( d + ( 13 * ( m + 1 ) / 5 ) + k + ( k / 4 ) + ( j / 4 ) + ( 5 * j ) ) % 7;
// Convert Zeller's value to ISO value (1 = Mon, ... , 7 = Sun )
int dayNum = ( ( h + 5 ) % 7 ) + 1;
return dayNum;
}
}
import java.util.*;
公共类日历显示{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);//扫描用户输入
System.out.print(“请输入1到12之间的月份(例如5):”;//提示用户输入月份
int m=input.nextInt();
System.out.print(“请输入完整年份(如2018):”;//提示用户输入年份
int y=input.nextInt();
//打印年度月份的日历
如果(m<1 | | m>12)
System.out.print(“输入错误!请重试”);
其他的
printMonthCalendar(m,y);
}
静态void printMonthCalendar(int m,int y){//以上述格式显示日历
int startDay=getStartDay(m,y);
int numDaysInMonths=getnumdaysinmonths(m,y);
printMonthHeader(m,y);
printMonthBody(开始日、每月数);
}
静态void printMonthBody(int startDay,int numDaysInMonths){//显示日历中的日期
int i;
对于(i=0;i我建议您使用。从了解有关现代日期时间API的更多信息
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Scan for user input
System.out.print("Please enter a month between 1 and 12 (e.g. 5): "); // Prompt user to enter month
int m = input.nextInt();
System.out.print("Please enter a full year (e.g. 2018): "); // Prompt user to enter year
int y = input.nextInt();
printMonth(y, m);
}
static void printMonth(int year, int month) {
YearMonth ym = YearMonth.of(year, month);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
int counter = 1;
// Get day of week of 1st date of the month and print space for as many days as
// distant from SUN
int dayValue = LocalDate.of(year, month, 1).getDayOfWeek().getValue();
for (int i = 0; i < dayValue; i++, counter++) {
System.out.printf("%-4s", "");
}
for (int i = 1; i <= ym.getMonth().length(ym.isLeapYear()); i++, counter++) {
System.out.printf("%-4d", i);
// Break the line if the value of the counter is multiple of 7
if (counter % 7 == 0) {
System.out.println();
}
}
}
}
注意:在中了解格式化打印。我认为您的getStartDay
方法不会返回您期望的星期几。它似乎从星期三返回1,到星期二返回7。示例:
Month Start day of week getStartDay()
------------------------------------------------
July 2020 Wednesday 1
August 2020 Saturday 4
September 2020 Tuesday 7
October 2020 Thursday 2
您的代码中可能还有一两个问题,但我认为您应该首先解决这个基本问题
这是一个很好的练习,您肯定在学习。对于生产代码,您永远不会实现Zeller算法或任何其他算法来查找月和周的长度。这是由标准库完成的,如Arvind Kumar Avinash的答案所示。请解释“问题”的详细信息你有。非常感谢你回复我。没有办法用我程序中的方法而不是像上面代码那样的单一方法来完成吗?对于生产代码,你应该更喜欢库方法而不是自己编写方法。作为练习,编写自己的方法很好,是的,这当然是可能的.
Month Start day of week getStartDay()
------------------------------------------------
July 2020 Wednesday 1
August 2020 Saturday 4
September 2020 Tuesday 7
October 2020 Thursday 2