在 Java 中打印 3x4 日历



我将不胜感激任何提示或建议。 我必须用 Java 打印一个年份日历,给定 1 月 1 日的年份和日期。我能够打印日历,但挑战是日历必须采用 3x4 格式(1 月、2 月、3 月并排,然后在下一个"行"上,4 月、5 月 6 月等。这是我目前自上而下打印日历的代码。我只是不知道从哪里开始。我只能使用选择结构、循环和方法。

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("January", 2, 20xx)返回" 7 8 9 10 11 12 13"foo("January", 5, 20xx)返回"28 29 30 31 "

这个函数可以让您一次循环打印一行,只需为必须打印月天的每一行调用它三次。从上面的代码来看,您应该能够实现此函数的逻辑以及其余的格式化,而不会有太大困难。

这里重要的是仔细考虑需要将哪些内容分离到其自身的功能中。在这种情况下,您需要灵活地从一个月中获取单个"行"(周)天数,因此您需要将其封装在其自己的函数中。

我认为关键点是你的开关情况: 它应该看起来像这样:

switch (month) 
{
case 1: 
System.out.print("                  January"); break;
case 2: 
System.out.print("                  February"); break;
case 3: 
System.out.println("                  March"); break;
case 4: 
System.out.print("                  April"); break;
case 5: 
System.out.print("                  May"); break;
case 6: 
System.out.println("                  June"); break;
case 7: 
System.out.print("                  July"); break;
case 8: 
System.out.print("                  August"); break;
case 9: 
System.out.println("                  September"); break;
case 10: 
System.out.print("                  October"); break;
case 11: 
System.out.print("                  November"); break;
case 12: 
System.out.println("                  December"); break;
}

最新更新