如何在java中显示日历
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35679827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display calendar in java
提问by Flinze
I'm currently doing a problem set where I have to create a calendar displaying all the months of a year, including the days of the month within it. However I'm having issues with the spacing of the first line of each month. In class we have only learned switch statements, if, else, while, do-while, for loops
我目前正在做一个问题集,我必须创建一个日历来显示一年中的所有月份,包括其中的月份。但是,我遇到了每个月第一行的间距问题。课上只学过switch语句,if,else,while,do-while,for循环
Here is what is currently displayed for one of my months:
以下是我一个月中当前显示的内容:
Image of outputIn the picture is doesn't show my inputs, but what I wrote was 2016 for the year and 5 for the weekday that the year starts in.
输出图像 图中没有显示我的输入,但我写的是 2016 年和 5 年开始的工作日。
Image of output of what what is desiredAgain, a picture of what is desired. I think my problem is possibly the equation I used which is: int firstDayEachMonth = (daysMonth + firstDayYear)%7; though the teacher gave this equation to us to use, but it seems like it doesn't work.
所期望的输出图像 再次,所期望的图像。我认为我的问题可能是我使用的等式: int firstDayEachMonth = (daysMonth + firstDayYear)%7; 虽然老师把这个方程给我们用了,但是好像不行。
As you can see the spaces on the first line is all the way to the left, it should be aligned with the designated dates, in this case for January, Jan 1st should align on Friday and Jan 2nd should align with Saturday but it is currently on Sunday and Monday.
如您所见,第一行的空格一直向左,它应该与指定的日期对齐,在这种情况下,对于 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();
}
}
}
采纳答案by The Roy
Can you try this example? I can see the following output:
你能试试这个例子吗?我可以看到以下输出:
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 29package general; import java.util.Scanner;
public class DisplayCalendar {
public static void main(String[] args) { int Y = 2016; // year int startDayOfMonth = 5; int spaces = startSayOfMonth; // 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(); } } }
回答by Ben Kamit
public class Exercice5_29DisplayCalenderDay {
/**
* @param args the command line arguments
*/
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.println("Enter the weekday that the year starts: ");
int day = input.nextInt();
int dayCounter = day;
int nbrOfDays = 0;
String monthx = "";
for (int month= 1; month <=12; month++)
{
// Switch to chose the month
switch (month)
{
case 1: monthx = "January";
nbrOfDays = 31;
break;
case 2: monthx = "February";
if ( year % 4 == 0 && year % 100 !=0 || year % 400 == 0)
{
nbrOfDays = 29;
break;
}
else
{ nbrOfDays = 28;
break;
}
case 3: monthx = "March";
nbrOfDays = 31;
break;
case 4: monthx = "April";
nbrOfDays = 30;
break;
case 5: monthx = "May";
nbrOfDays = 31;
break;
case 6: monthx = "June";
nbrOfDays = 30;
break;
case 7: monthx = "July";
nbrOfDays = 31;
break;
case 8: monthx = "August";
nbrOfDays = 31;
break;
case 9: monthx = "September";
nbrOfDays = 30;
break;
case 10: monthx = "October";
nbrOfDays = 31;
break;
case 11: monthx = "November";
nbrOfDays = 30;
break;
case 12: monthx = "December";
nbrOfDays = 31;
break;
}
System.out.printf("%15s %d \n", monthx , year);
System.out.println("----------------------------");
System.out.printf("%s %s %s %s %s %s %s\n ", "Sun","Mon","Tue", "Wed", "Thu","Fri","Sat");
for (int space =1; space<=day; space++)
{
System.out.printf("%4s", " ");
}
for (int i = 1; i <=nbrOfDays; i++)
{
dayCounter++;
if ( dayCounter% 7==0)
System.out.printf("%- 4d\n", i);
else
System.out.printf("%-4d", i);
}
day = (day + nbrOfDays)%7;
System.out.println();
}
}
}
回答by Luan Moraes
Since this seems to be a homework, I won't bother to give you the right algorithm. That would defeat the purpose of you - or other people with the same problem - practice your programming and analytical skills.
由于这似乎是一项家庭作业,因此我不会费心为您提供正确的算法。这将违背你——或其他有同样问题的人——练习你的编程和分析技能的目的。
In this line for (int space = 1; space <= firstDayEachMonth; space++)
you can totally ignore firstDayEachMonth
result and use your firstDayYear
counter. This counter has the starting weekday of the year and it is incremented each day that passes. Also, it is necessary to define if your week starts in 0 or 1.
在这一行中,for (int space = 1; space <= firstDayEachMonth; space++)
您可以完全忽略firstDayEachMonth
结果并使用您的firstDayYear
计数器。该计数器具有一年中的起始工作日,并且它在经过的每一天递增。此外,有必要定义您的一周是从 0 还是 1 开始。
In this part, you are already setting a line break for the end of the week here in:
在这部分中,您已经在此处设置了本周末的换行符:
if (firstDayYear%7 == 0)
System.out.println();
I would reset firstDayYear
when you reach this condition because since you are using it as parameter to set your spaces, you'll never have this number going greater than 7. This way you have each week line laid out correctly on the calendar and the only problem would be presenting it in the right format on the screen.
firstDayYear
当你达到这个条件时,我会重置,因为你使用它作为参数来设置你的空间,你永远不会让这个数字大于 7。这样你就可以在日历上正确布置每周的行,唯一的问题将在屏幕上以正确的格式呈现它。
When you print the days of the week header like this System.out.println("Sun Mon Tue Wed Thu Fri Sat");
mind that you have the names with length 3 plus 5 whitespaces. So this line System.out.printf("%3d ", daysDisplay);
should have a digit with a width of 3 spaces, which explains the use of %3d
, plus 5 whitespaces. In this case you have 6 whitespace that you always give you the wrong offset and will cause some hell on some lines.
当您像这样打印星期几标题时System.out.println("Sun Mon Tue Wed Thu Fri Sat");
,您会想到名称长度为 3 加 5 个空格。所以这一行System.out.printf("%3d ", daysDisplay);
应该有一个宽度为 3 个空格的数字,这解释了%3d
, 加上 5 个空格的用法。在这种情况下,你有 6 个空格,你总是给你错误的偏移量,并且会在某些行上造成一些麻烦。
These are the things I've noticed and I hope it helps. Peace!
这些是我注意到的事情,我希望它有所帮助。和平!