2016-02-28 70 views
1

我目前正在做一个问题集,我必须创建一个日历来显示一年中的所有月份,包括其中的月份的日期。但是,我遇到了每月第一行的间隔问题。在课堂上,我们只了解到switch语句,如果,否则,同时,做,而对于循环如何在java中显示日历

这里是目前显示的我的一个月份:

Image of output 在图片中是不显示我的输入,但我写的是2016年的年份和5年的平均年份开始英寸

Image of output of what what is desired 再次,一个什么是想要的图片。我认为我的问题可能是我使用的公式:int firstDayEachMonth =(daysMonth + firstDayYear)%7;尽管老师给了我们这个等式来使用,但它似乎不起作用。

正如你所看到的,第一行的空格是左侧的,它应该与指定日期对齐,在这种情况下,对于1月份,1月1日应该在星期五和1月2日对齐,并与星期六保持一致但目前在星期日和星期一。

import java.util.Scanner; 

    public class DisplayCalendar 
    { 
    public static void main(String[] args) 
    { 
    //Create a new scanner 
    Scanner input = new Scanner(System.in); 

    // Prompt user to enter year 
    System.out.print("Enter a year: "); 
    int year = input.nextInt(); 

    // Prompt user to enter first day of the year 
    System.out.print("Enter the weekday that the year starts: "); 
    int firstDayYear = input.nextInt(); 

    // A for loop that prints out each month 
    for(int month = 1; month <= 12; month++) 
    { 
     // Set the value of the amount of days in a month 
     int daysMonth = 0; 

     // Set value of the month 
     String monthDisplay = ""; 

     // Find name of each month and number of days 
     switch(month) 
     { 
      case 1: monthDisplay = "January"; 
       daysMonth = 31; 
       break; 

      case 2: 
       monthDisplay = "February"; 
       int leapYear = 0; 
       while (leapYear > -1) 
       { 
        // Count all years that are divisible by 4 to be a leap year. 
        leapYear += 4; 

        // If the year inputted is a leap year, the days of the month will be 29. 
        if (year == leapYear) 
        { 
         daysMonth = 29; 
         break; 
        } 

        else 
        { 
         daysMonth = 28; 
        } 
       } 
       break; 

      case 3: monthDisplay = "March"; 
       daysMonth = 31; 
       break; 

      case 4: monthDisplay = "April"; 
       daysMonth = 30; 
       break; 

      case 5: monthDisplay = "May"; 
       daysMonth = 31; 
       break; 

      case 6: monthDisplay = "June"; 
       daysMonth = 30; 
       break; 

      case 7: monthDisplay = "July"; 
       daysMonth = 31; 
       break; 

      case 8: monthDisplay = "August"; 
       daysMonth = 31; 
       break; 

      case 9: monthDisplay = "September"; 
       daysMonth = 30; 
       break; 

      case 10: monthDisplay = "October"; 
       daysMonth = 31; 
       break; 

      case 11: monthDisplay = "November"; 
       daysMonth = 30; 
       break; 

      case 12: monthDisplay = "December"; 
       daysMonth = 31; 
       break; 

      // If the month is not recognized, dialog box will be displayed, and then exits program. 
      default : System.out.print("Invalid: Your month is not recognized. "); 
       System.exit(0); 

     } 
     // Display the month and year 
     System.out.println("      "+ monthDisplay + " " + year); 

     // Display the lines 
     System.out.println("_____________________________________"); 

     // Display the days of the week 
     System.out.println("Sun  Mon  Tue  Wed  Thu  Fri  Sat"); 

     // Print spaces depending on the day the month starts. 
     int firstDayEachMonth = (daysMonth + firstDayYear)%7; 
     for (int space = 1; space <= firstDayEachMonth; space++) 
      System.out.print(" "); 

     // Print the days 
     for (int daysDisplay = 1; daysDisplay <= daysMonth; daysDisplay++) 
     { 
      if (firstDayYear%7 == 0) 
       System.out.println(); 

      System.out.printf("%3d  ", daysDisplay); 
      firstDayYear += 1; 
     } 
     System.out.println(); 
    } 

} 

}

+0

假设这是功课,你不准看标准的Java库和API?如果你是,java.util.Calendar和java.lang.String.format可能是有趣的。特别是,Calendar.roll将帮助你完成一大堆工作... –

+0

嘿,我们不能使用不同的库,只能保持扫描仪。 – Flinze

回答

0

你可以试试这个例子吗? 我可以看到下面的输出:

 
    February 2016 
    Sun Mon Tue Wed Thu Fri Sat 
     1 2 3 4 5 6 
    7 8 9 10 11 12 13 
    14 15 16 17 18 19 20 
    21 22 23 24 25 26 27 
    28 29

package general; 

import java.util.Scanner; 

公共类DisplayCalendar {

public static void main(String[] args) { int Y = 2016; // year int startDayOfMonth = 5; int spaces = startSayOfMonth; // months[i] = name of month i String[] months = { "", // leave empty so that we start with months[1] = "January" "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // days[i] = number of days in month i int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; for (int M = 1; M <= 12; M++) { // check for leap year if ((((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)) && M == 2) days[M] = 29; // print calendar header // Display the month and year System.out.println(" "+ months[M] + " " + Y); // Display the lines System.out.println("_____________________________________"); System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); // spaces required spaces = (days[M-1] + spaces)%7; // print the calendar for (int i = 0; i < spaces; i++) System.out.print(" "); for (int i = 1; i <= days[M]; i++) { System.out.printf(" %3d ", i); if (((i + spaces) % 7 == 0) || (i == days[M])) System.out.println(); } System.out.println(); } }

}

+0

如果您想使用扫描仪,请使用它来获取月份和年份。让我知道这是否解决了你的问题。 – Learner

+0

感谢您的回复,我们还没有学习方法,所以我不认为我们被允许在我们的代码中实现它。 – Flinze

+0

它不能解决我的问题。 – Flinze

0
public class Exercice5_29DisplayCalenderDay { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
     //Create a new scanner 
    Scanner input = new Scanner(System.in); 

    // Prompt user to enter year 
    System.out.print("Enter a year: "); 
    int year = input.nextInt(); 

    // Prompt user to enter first day of the year 
    System.out.println("Enter the weekday that the year starts: "); 
    int day = input.nextInt(); 
    int dayCounter = day; 
    int nbrOfDays = 0; 
    String monthx = ""; 
    for (int month= 1; month <=12; month++) 
    { 

         // Switch to chose the month 
       switch (month) 
      { 
        case 1: monthx = "January"; 
          nbrOfDays = 31; 
          break; 
        case 2: monthx = "February"; 
            if (year % 4 == 0 && year % 100 !=0 || year % 400 == 0) 
         { 
          nbrOfDays = 29; 
          break; 
         } 
            else 
         { nbrOfDays = 28; 
          break; 
         } 
       case 3: monthx = "March"; 
          nbrOfDays = 31; 
          break; 
        case 4: monthx = "April"; 
          nbrOfDays = 30; 
          break; 
         case 5: monthx = "May"; 
          nbrOfDays = 31; 
          break; 

         case 6: monthx = "June"; 
          nbrOfDays = 30; 
          break; 
          case 7: monthx = "July"; 
          nbrOfDays = 31; 
          break; 
          case 8: monthx = "August"; 
          nbrOfDays = 31; 
          break; 
        case 9: monthx = "September"; 
          nbrOfDays = 30; 
          break; 
        case 10: monthx = "October"; 
          nbrOfDays = 31; 
          break; 
        case 11: monthx = "November"; 
          nbrOfDays = 30; 
          break; 
         case 12: monthx = "December"; 
          nbrOfDays = 31; 
          break;     
      } 

       System.out.printf("%15s %d \n", monthx , year); 
       System.out.println("----------------------------"); 
       System.out.printf("%s %s %s %s %s %s %s\n ", "Sun","Mon","Tue", "Wed", "Thu","Fri","Sat"); 

       for (int space =1; space<=day; space++) 
       { 
        System.out.printf("%4s", " "); 
       } 
       for (int i = 1; i <=nbrOfDays; i++) 
       { 
        dayCounter++; 
        if (dayCounter% 7==0) 
        System.out.printf("%- 4d\n", i); 
        else 
        System.out.printf("%-4d", i); 

       } 
       day = (day + nbrOfDays)%7; 

         System.out.println(); 

    }  

} 
}