使用Java打印3x4日历(1月后的开始日期问题)

时间:2017-04-15 17:48:09

标签: java calendar

我的目标是打印一个日历(1月2月3月,然后4月5月6月在另一行等)1月是唯一完美运作的月份。我的问题是:1月之后的每个月的开始日(如果Jan在星期一结束,2月必须在星期二开始),并且每个月的最后一周的日期没有正确放置。另一个小问题是代码的长度,对于我想要做的事情似乎太长/多余。我只限于选择结构,循环和模块。任何帮助或提示都表示赞赏。

import javax.swing.JOptionPane;
public class Assignment4
{
  public static int startDates(int day, int month)
 {
  //calculate start dates for Feb-Dec, Based on previous months end day,possibly dependant on lastDayM
//status: not working
int i;
int startDay = 7-day;
return startDay;
 }
public static int lastDayM(int month, int year)
{
//calculate last day for every month
//status: working 
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(simple leap year)
    if (year % 4 == 0)
      lastDay = lastDay + 29;
    else
      lastDay = lastDay + 28;
  }
  }
  return lastDay;
 }

public static void displayHeader(int month)
 {
//display the month headers in a 3x4 format
//status:working
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;
  }
}

public static int displayWeek(int month, int weekn, int lastDay, int day)
{
//status: almost working for every case. Only issue is the last week
//display the months one week at a time
int startDay = startDates(day, month);
int d = 0;
switch (weekn)
{
  case 1:
  {          
    for (int b = 1; b <= day; b++) 
    {
      System.out.print("    ");
    }
    for (d=1;d<=7-day;d++)
    {
      System.out.print("   "+d);
    }
    if(month!=1)
      day=7-(d%7);
    break;
  }
  case 2:
  {
    for(d=8-day;d<=14-day;d++)
    {
      if(d<10)
      {
        System.out.print("   "+d);
      }
      else
      {
        System.out.print("  "+d);
      }
    }
    break;
  }
  case 3:
  {
    for(d=15-day;d<=21-day;d++)
    {
      System.out.print("  "+d);
    }
    break;
  }
  case 4:
  {
    for(d=22-day;d<=28-day;d++)
    {
      System.out.print("  "+d);
    }
    break;
  }
  case 5:
  {
    for(d=29-day;d<=lastDay;d++)
    {
      System.out.print("  "+d);
    }
    if(lastDay%7==3)
      System.out.print("                ");
    if(lastDay%7==1)
      System.out.print("                        ");
    break;
  }
}
return d;
}
public static void main(String args[])
{
//declaration
int i, row, col, j, year, day, month;
String yearstr, daystr;

//input
yearstr = JOptionPane.showInputDialog("Enter a year: ");
year = Integer.parseInt(yearstr);

daystr = JOptionPane.showInputDialog("Enter a day for Jan.1: 0-Su, 1-M, 2-M, etc.");
day = Integer.parseInt(daystr);

System.out.println("                                              "+year);


//display month names
for(j=1;j<=3;j++)
{
  displayHeader(j);
}
//week header
for(i=1;i<=3;i++)
{
  System.out.print("   Su  M   Tu  W  Th   F   S");
  System.out.print("   ");
}
System.out.println();
//display a row of 3 months
for(row=1;row<=5;row++)
{
  for(col=1;col<=3;col++)
  {
    displayWeek(col,row,lastDayM(col,year),day);
    System.out.print("   ");
  }
  System.out.println();
}
System.out.println();

//2nd set of months
for(j=4;j<=6;j++)
{
  displayHeader(j);
}
for(i=1;i<=3;i++)
{
  System.out.print("   Su  M   Tu  W  Th   F   S");
  System.out.print("   ");
}
System.out.println();
for(row=1;row<=5;row++)
{
  for(col=4;col<=6;col++)
  {
    displayWeek(col,row,lastDayM(col,year),day);
    System.out.print("   ");
  }
  System.out.println();
}
System.out.println();

//3rd set of months
for(j=7;j<=9;j++)
{
  displayHeader(j);
}
for(i=1;i<=3;i++)
{
  System.out.print("   Su  M   Tu  W  Th   F   S");
  System.out.print("   ");
}
System.out.println();
for(row=1;row<=5;row++)
{
  for(col=7;col<=9;col++)
  {
    displayWeek(col,row,lastDayM(col,year),day);
    System.out.print("   ");
  }
  System.out.println();
}
System.out.println();

//last set
for(j=10;j<=12;j++)
{
  displayHeader(j);
}
for(i=1;i<=3;i++)
{
  System.out.print("   Su  M   Tu  W  Th   F   S");
  System.out.print("   ");
}
System.out.println();
for(row=1;row<=5;row++)
{
  for(col=10;col<=12;col++)
  {
    displayWeek(col,row,lastDayM(col,year),day);
    System.out.print("   ");
  }
  System.out.println();
  }
 }
}

1 个答案:

答案 0 :(得分:0)

你的代码有几个错误,就像你说的那样,它有一些冗余的代码。

main方法中,重复每行的代码四次。这占用了四倍的空间,增加了拼写错误的机会。相反,您可以创建一个循环for(int row=0; row<4; row++),然后让代码重复四次。

public static void main(String args[]){
    //Get input
    int year = Integer.parseInt(JOptionPane.showInputDialog("Enter a year: "));
    int day = Integer.parseInt(JOptionPane.showInputDialog("Enter a day for Jan 1: 0-Sun, 1-Mon, 2-Tues, etc."));

    //Print year
    System.out.println("                                              " + year);

    //For each of the four rows
    for(int row=0; row<4; row++){
        //Display the Month headers
        for(int i=0; i<3; i++){
            displayHeader(3*row + i);
        }

        //Display the Week headers
        for(int i=0; i<3; i++){
            System.out.print("   Su  M   Tu  W  Th   F   S");
            System.out.print("   ");                
        }           
        System.out.println();

        //Display the rows of each month
        for(int week=0; week<5; week++){
            for(int month=0; month<3; month++){
                displayWeek(3*row + month, week, firstDayM(3*row + month, year, day), lastDayM(3*row + month, year));
                System.out.print("   ");
            }
            System.out.println();
        }
        System.out.println();                       
    }
}

请注意,我更改了int代码的设置,以便0 = 1月,1 = 2月,依此类推。我还将一周的日期更改为相同的格式(即0 =星期一,1 =星期二等)。保持一致总是好的。我稍微调整了displayHeader()方法以反映这种变化。

public static void displayHeader(int month){
    //display the month headers in a 3x4 format
    //status:working
    switch (month) {
        case 0: 
            System.out.print("            January"); break;
        case 1:
            System.out.print("                         February"); break;
        case 2: 
            System.out.println("                       March"); break;
        case 3: 
            System.out.print("             April"); break;
        case 4: 
            System.out.print("                           May"); break;
        case 5: 
            System.out.println("                            June"); break;
        case 6: 
            System.out.print("             July"); break;
        case 7: 
            System.out.print("                           August"); break;
        case 8: 
            System.out.println("                        September"); break;
        case 9: 
            System.out.print("           October"); break;
        case 10: 
            System.out.print("                          November"); break;
        case 11: 
            System.out.println("                      December"); break;
    }
}

您的lastDayM方法有效;我只是对它进行了简化,并添加了一个firstDayM方法,该方法可以从1月1日开始计算一周中的第一天并计算出来。

public static int lastDayM(int month, int year){
    //calculate last day for every month
    //status: working 
    if (month == 3 || month == 5 || month == 8 || month == 10)
        return 30;
    if(month == 1){
        if (year % 4 == 0){
            return 29;
        }
        return 28;
    }
    return 31;
}

public static int firstDayM(int month, int year, int janOneDay){
    int firstDayM = janOneDay;

    for(int i=0; i<month; i++){
        firstDayM += lastDayM(i, year);
    }

    return firstDayM % 7;
}

您的displayWeek()方法是我花费最多时间查看的方法,因为您的算法对我来说没有多大意义。最后,我添加了一个参数firstDay,并使该方法遵循以下步骤

  • 如果是第一周,则根据月份开始的星期几(来自参数firstDay
  • 来抵消日历
  • 每个后续周,只需添加更多数字
  • 上周,只能到lastDay;然后根据需要进行偏移

    public static void displayWeek(int month, int week, int firstDay, int lastDay){
    //status: almost working for every case. Only issue is the last week
    //display the months one week at a time
    switch (week){
        case 0:
            for (int b=0; b<firstDay; b++) {
                System.out.print("    ");
            }
            for (int d=1; d<8-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 1:
            for (int d=8-firstDay; d<15-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 2:
            for (int d=15-firstDay; d<22-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 3:
            for (int d=22-firstDay; d<29-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 4:
            if(lastDay == 28 && firstDay==0){
                System.out.print("                        ");
                break;
            }
            for (int d=29-firstDay; d<36-firstDay; d++){
                if(d<lastDay+1){
                    System.out.print((d<10 ? "   " : "  ")  + d);
                }else{
                    System.out.print("    ");
                }
            }
            break;
    }
    }
    

另请注意,虽然这不是您提出的问题的一部分,但您的应用程序将在超过五周(例如本月,巧合)的月份内无法工作。

以下是最终代码,供参考:

import javax.swing.JOptionPane;

public class Assignment4{   
public static int lastDayM(int month, int year){
    //calculate last day for every month
    //status: working 
    if (month == 3 || month == 5 || month == 8 || month == 10)
        return 30;
    if(month == 1){
        if (year % 4 == 0){
            return 29;
        }
        return 28;
    }
    return 31;
}

public static int firstDayM(int month, int year, int janOneDay){
    int firstDayM = janOneDay;

    for(int i=0; i<month; i++){
        firstDayM += lastDayM(i, year);
    }

    return firstDayM % 7;
}

public static void displayHeader(int month){
    //display the month headers in a 3x4 format
    //status:working
    switch (month) {
        case 0: 
            System.out.print("            January"); break;
        case 1:
            System.out.print("                         February"); break;
        case 2: 
            System.out.println("                       March"); break;
        case 3: 
            System.out.print("             April"); break;
        case 4: 
            System.out.print("                           May"); break;
        case 5: 
            System.out.println("                            June"); break;
        case 6: 
            System.out.print("             July"); break;
        case 7: 
            System.out.print("                           August"); break;
        case 8: 
            System.out.println("                        September"); break;
        case 9: 
            System.out.print("           October"); break;
        case 10: 
            System.out.print("                          November"); break;
        case 11: 
            System.out.println("                      December"); break;
    }
}

public static void displayWeek(int month, int week, int firstDay, int lastDay){
    //status: almost working for every case. Only issue is the last week
    //display the months one week at a time
    switch (week){
        case 0:
            for (int b=0; b<firstDay; b++) {
                System.out.print("    ");
            }
            for (int d=1; d<8-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 1:
            for (int d=8-firstDay; d<15-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 2:
            for (int d=15-firstDay; d<22-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 3:
            for (int d=22-firstDay; d<29-firstDay; d++){
                System.out.print((d<10 ? "   " : "  ")  + d);
            }
            break;
        case 4:
            if(lastDay == 28 && firstDay==0){
                System.out.print("                        ");
                break;
            }
            for (int d=29-firstDay; d<36-firstDay; d++){
                if(d<lastDay+1){
                    System.out.print((d<10 ? "   " : "  ")  + d);
                }else{
                    System.out.print("    ");
                }
            }
            break;
    }
}

public static void main(String args[]){
    //Get input
    int year = Integer.parseInt(JOptionPane.showInputDialog("Enter a year: "));
    int day = Integer.parseInt(JOptionPane.showInputDialog("Enter a day for Jan 1: 0-Sun, 1-Mon, 2-Tues, etc."));

    //Print year
    System.out.println("                                              " + year);

    //For each of the four rows
    for(int row=0; row<4; row++){
        //Display the Month headers
        for(int i=0; i<3; i++){
            displayHeader(3*row + i);
        }

        //Display the Week headers
        for(int i=0; i<3; i++){
            System.out.print("   Su  M   Tu  W  Th   F   S");
            System.out.print("   ");                
        }           
        System.out.println();

        //Display the rows of each month
        for(int week=0; week<5; week++){
            for(int month=0; month<3; month++){
                displayWeek(3*row + month, week, firstDayM(3*row + month, year, day), lastDayM(3*row + month, year));
                System.out.print("   ");
            }
            System.out.println();
        }
        System.out.println();                       
    }
}
}