2017-04-15 27 views
-1

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

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(); 
    } 
} 
} 
+0

在你的代码,是1月1日或二月?另外,是星期一还是星期二? –

+0

1是1月份,0是星期日,1是星期一等 – Asgore

+0

我正在尝试阅读您的代码,但它很混乱。 'startDates()'存在,但它从来没有使用过。 。 。 –

回答

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();      
    } 
} 
} 
+0

谢谢,我会花时间学习这些代码来理解它,希望将来我会减少这些错误。感谢您花时间。 – Asgore

+0

@Asgore不客气。随意问任何问题。 –

相关问题