用Java打印3x4日历

时间:2017-04-14 01:25:51

标签: java calendar

我将不胜感激任何提示或建议。  鉴于今年和1月1日的日期,我必须用Java打印一年的日历。我能够打印日历,但挑战是日历必须是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);
  }
} 

2 个答案:

答案 0 :(得分:1)

考虑一下您的限制(在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 "

这一功能可让您一次循环打印一行,只需在每行打印几次的情况下调用三次即可。天。你应该能够实现这个函数的逻辑,其余的格式化,没有太大的困难,从上面的代码判断。

这里重要的是要仔细考虑需要分成什么功能的东西。在这种情况下,您需要灵活地获得单个&#34;行&#34; (一周)从一个月开始,所以你需要将它封装在自己的函数中。

答案 1 :(得分:0)

我认为关键点在于您的开关案例: 看起来应该是这样的:

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;
    }