如果有任何提示或建议,我将不胜感激。 我必须用 Java 打印一个年份日历,给出 1 月 1 日的年份和日期。我可以打印日历,但挑战是日历必须采用 3x4 格式(一月、二月、三月并排,然后在下一个“行”四月、五月、六月等。这是我在从上到下打印日历的时刻。我只是不知道从哪里开始。我只能使用选择结构、循环和方法。
import javax.swing.JOptionPane;
public class Assignment4DONOTCHANGE
{
//get headers set up to be printed in main
public static void printHeader(int month)
{
switch (month)
{
case 1:
System.out.println(" January"); break;
case 2:
System.out.println(" February"); break;
case 3:
System.out.println(" March"); break;
case 4:
System.out.println(" April"); break;
case 5:
System.out.println(" May"); break;
case 6:
System.out.println(" June"); break;
case 7:
System.out.println(" July"); break;
case 8:
System.out.println(" August"); break;
case 9:
System.out.println(" September"); break;
case 10:
System.out.println(" October"); break;
case 11:
System.out.println(" November"); break;
case 12:
System.out.println(" December"); break;
}
// Display header and days of the week
System.out.println("---------------------------------------------");
System.out.println(" Su M Tu W Th F S");
System.out.println();
}
//compute last day of each month
public static int lastDayM(int month, int year)
{
//reset each iteration
int lastDay = 0;
if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||month == 12)
lastDay = lastDay + 31;
else
{
if (month == 4 || month == 6 || month == 9 || month == 11)
lastDay = lastDay + 30;
else
{ // Test for leap year
if (year % 4 == 0)
lastDay = lastDay + 29;
else
lastDay = lastDay + 28;
}
}
return lastDay;
}
public static void main(String args[])
{
//declaration
String yearstr, daystr;
int year, day, lastDay;
// Prompt the user to enter the year and first day of the year
yearstr = JOptionPane.showInputDialog("Enter a year: ");
year = Integer.parseInt(yearstr);
daystr = JOptionPane.showInputDialog("Enter a day for Jan.1: 0-S, 1-M, 2-Tu, etc.");
day = Integer.parseInt(daystr);
System.out.println(" "+year);
int month;
for (month = 1; month <= 12; month++)
{
printHeader(month);
// Compute beginning day of the week
day = day % 7;
for (int b = 1; b <= day * 7; b++)
{
System.out.print(" ");
}
// Compute last day of present month
lastDay = lastDayM(month, year);
// Display calender for current month
int d;
for (d = 1; d <= lastDay; d++)
{
// Add a black space before numbers less than 10
if (d < 10)
System.out.print(" ");
// Start new line after satuarday
if (day % 7 == 6)
{
System.out.print(d+" ");
System.out.println();
System.out.println();
}
else
{
System.out.print(d + " ");
// After last day of the month go to new line
if (d == lastDay)
System.out.println();
}
day = day + 1;
}
System.out.println();
}
System.exit(0);
}
}
最佳答案
考虑一下您的限制(在 3x4 网格中打印,没有花哨的字符串格式帮助)意味着什么:您必须一次打印一行。这意味着您将打印前三个月,然后打印它们的标题行和一周中的日期(对于所有三个在一行中),然后打印 4-5 行编号的日期,其中每行包含每个月份的一周三个月。
综上所述,这确实是一个程序设计和逻辑分离的问题。例如,如果有一个函数可以接受一年、一个月和一周的数字,并返回该周需要打印的字符串,那就太好了。例如。对于从星期一开始的一年,您希望 foo("January", 1, 20xx)
返回 "1 2 3 4 5 6"
和 foo("一月", 2, 20xx)
返回 "7 8 9 10 11 12 13"
和
foo("January", 5, 20xx)
返回 "28 29 30 31 "
这个函数可以让您一次循环打印一行,只需为必须打印月日的每一行调用它三次即可。从上面的代码来看,您应该能够实现此函数的逻辑以及其余的格式化,没有太多困难。
这里重要的是仔细考虑哪些内容需要分离成自己的功能。在这种情况下,您需要灵活地获取一个月中的单“行”(周)天数,因此您需要将其封装在自己的函数中。
关于java - 用 Java 打印 3x4 日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43403986/