我目前正在做一个问题集,必须创建一个日历来显示一年中的所有月份,包括其中的月份。但是我每个月的第一行的间距有问题。在课堂上,我们只学习了switch语句,如果是,则为while循环
这是我一个月中当前显示的内容:
输出的图片图中没有显示我的输入,但是我写的是该年的2016年和该年开始的工作日的5岁。
所需内容输出的图像再次显示所需内容。我认为我的问题可能是我使用的公式是:int firstDayEachMonth =(daysMonth + firstDayYear)%7; 尽管老师将此方程式提供给我们使用,但似乎不起作用。
如您所见,第一行的空格一直在左侧,应该与指定的日期对齐,在这种情况下,对于1月,1月1日应该与星期五对齐,1月2日应该与星期六对齐,但是当前在星期日和星期一。
import java.util.Scanner;
public class DisplayCalendar
{
public static void main(String[] args)
{
//Create a new scanner
Scanner input = new Scanner(System.in);
// Prompt user to enter year
System.out.print("Enter a year: ");
int year = input.nextInt();
// Prompt user to enter first day of the year
System.out.print("Enter the weekday that the year starts: ");
int firstDayYear = input.nextInt();
// A for loop that prints out each month
for(int month = 1; month <= 12; month++)
{
// Set the value of the amount of days in a month
int daysMonth = 0;
// Set value of the month
String monthDisplay = "";
// Find name of each month and number of days
switch(month)
{
case 1: monthDisplay = "January";
daysMonth = 31;
break;
case 2:
monthDisplay = "February";
int leapYear = 0;
while (leapYear > -1)
{
// Count all years that are divisible by 4 to be a leap year.
leapYear += 4;
// If the year inputted is a leap year, the days of the month will be 29.
if (year == leapYear)
{
daysMonth = 29;
break;
}
else
{
daysMonth = 28;
}
}
break;
case 3: monthDisplay = "March";
daysMonth = 31;
break;
case 4: monthDisplay = "April";
daysMonth = 30;
break;
case 5: monthDisplay = "May";
daysMonth = 31;
break;
case 6: monthDisplay = "June";
daysMonth = 30;
break;
case 7: monthDisplay = "July";
daysMonth = 31;
break;
case 8: monthDisplay = "August";
daysMonth = 31;
break;
case 9: monthDisplay = "September";
daysMonth = 30;
break;
case 10: monthDisplay = "October";
daysMonth = 31;
break;
case 11: monthDisplay = "November";
daysMonth = 30;
break;
case 12: monthDisplay = "December";
daysMonth = 31;
break;
// If the month is not recognized, dialog box will be displayed, and then exits program.
default : System.out.print("Invalid: Your month is not recognized. ");
System.exit(0);
}
// Display the month and year
System.out.println(" "+ monthDisplay + " " + year);
// Display the lines
System.out.println("_____________________________________");
// Display the days of the week
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
// Print spaces depending on the day the month starts.
int firstDayEachMonth = (daysMonth + firstDayYear)%7;
for (int space = 1; space <= firstDayEachMonth; space++)
System.out.print(" ");
// Print the days
for (int daysDisplay = 1; daysDisplay <= daysMonth; daysDisplay++)
{
if (firstDayYear%7 == 0)
System.out.println();
System.out.printf("%3d ", daysDisplay);
firstDayYear += 1;
}
System.out.println();
}
}
}
你可以试试这个例子吗?我可以看到以下输出:
February 2016
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
package general;
import java.util.Scanner;
public class DisplayCalendar {
public static void main(String[] args) {
int Y = 2016; // year
int startDayOfMonth = 5;
int spaces = startDayOfMonth;
// startDayOfMonth
// months[i] = name of month i
String[] months = {
"", // leave empty so that we start with months[1] = "January"
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// days[i] = number of days in month i
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
for (int M = 1; M <= 12; M++) {
// check for leap year
if ((((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)) && M == 2)
days[M] = 29;
// print calendar header
// Display the month and year
System.out.println(" "+ months[M] + " " + Y);
// Display the lines
System.out.println("_____________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
// spaces required
spaces = (days[M-1] + spaces)%7;
// print the calendar
for (int i = 0; i < spaces; i++)
System.out.print(" ");
for (int i = 1; i <= days[M]; i++) {
System.out.printf(" %3d ", i);
if (((i + spaces) % 7 == 0) || (i == days[M])) System.out.println();
}
System.out.println();
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句