[英]I'm trying to do a calendar program using Java but when it prints, the start of month is wrong and it messed up the output
I'm a beginner in programming and I am making a Java program for calendar.我是编程初学者,正在为日历制作 Java 程序。 I don't know how to make the start of the month on a certain day.
我不知道如何在某一天制作月初。 As an example, I want the start of November 2022 to be on Tuesday.
例如,我希望 2022 年 11 月的开始时间是星期二。 My current code messed up the output, please help.
我当前的代码弄乱了输出,请帮忙。 Here's my code:
这是我的代码:
import java.util.Scanner;
public class printCalendar {
//calculating the month's days
public static int dayOfMonth(int mon, int day, int yr) {
int y = yr - (14 - mon) / 12;
int x = y + y/4 - y/100 + y/400;
int m = mon + 12 * ((14 - mon) / 12) - 2;
int d = (day + x + (31*m)/12) % 7;
return d;
}
//function for leap year detection
public static boolean leapYear(int yr) {
if ((yr % 4 == 0) && (yr % 100 != 0))
return true;
if (yr % 400 == 0)
return true;
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter full year (e.g., 2022): ");
int yr = scan.nextInt();
System.out.print("Enter month as a number between 1 and 12: ");
int mon = scan.nextInt();
String[] month = {"",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// days of a month ( maximum 31 )
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// Leap year checking
if (mon == 2 && leapYear(yr)) days[mon] = 29;
// month name and days name
System.out.println(" " + month[mon] + " " + yr);
System.out.println("----------------------------");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
// first day
int d = dayOfMonth(mon, 1, yr);
// printing the calendar
for (int i = 0; i < d; i++)
System.out.print(" ");
for (int i = 1; i <= days[mon]; i++) {
System.out.printf("%2d ", i);
if (((i + d) % 7 == 0) || (i == days[mon])) System.out.println();
}
}
}
Here's the example output if I want the program to display November 2022 using the code I attached.如果我希望程序使用我附加的代码显示 2022 年 11 月,这里是示例输出。 I want the start of the month to be Tuesday (like a real calendar) instead of Sunday.
我希望这个月的开始是星期二(就像一个真正的日历)而不是星期日。 enter image description here
在此处输入图像描述
I suggested you can try the way to format a string with printf
or stirng.format
to correct messy output.我建议您可以尝试使用
printf
或stirng.format
格式化字符串的方式来纠正混乱的输出。 I've verified the code you posted and used the printf
method to gave a space of 4 character for every words.我已经验证了您发布的代码并使用
printf
方法为每个单词提供了 4 个字符的空间。
If you would like to see the effect, the below is what I'm trying.如果你想看到效果,下面是我正在尝试的。
import java.util.Scanner;
public class StackOverFlowTest {
//calculating the month's days
public static int dayOfMonth(int mon, int day, int yr) {
int y = yr - (14 - mon) / 12;
int x = y + y/4 - y/100 + y/400;
int m = mon + 12 * ((14 - mon) / 12) - 2;
int d = (day + x + (31*m)/12) % 7;
return d;
}
//function for leap year detection
public static boolean leapYear(int yr) {
if ((yr % 4 == 0) && (yr % 100 != 0))
return true;
return yr % 400 == 0;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter full year (e.g., 2022): ");
int yr = scan.nextInt();
System.out.print("Enter month as a number between 1 and 12: ");
int mon = scan.nextInt();
String[] month = {"",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// days of a month ( maximum 31 )
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// Leap year checking
if ( mon== 2 && leapYear(yr)) days[mon] = 29;
// month name and days name
System.out.println(" " + month[mon] + " " + yr);
System.out.println("----------------------------");
String[] dayOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// first day
int d = dayOfMonth(mon, 1, yr);
// printing the dayOfWeek
for (String s : dayOfWeek) {
System.out.printf("%-4s", s);
}
System.out.printf("%n");
// printing the calendar
for (int i = 0; i < d; i++)
System.out.printf("%4s", "");
for (int i = 1; i <= days[mon]; i++) {
System.out.printf("%-4d", i);
if (((i + d) % 7 == 0) || (i == days[mon])) System.out.println();
}
}
}
OUTPUT:输出:
Enter full year (e.g., 2022): 2022
Enter month as a number between 1 and 12: 11
November 2022
----------------------------
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.