2015-09-27 59 views
1

嗨,大家好,我真的需要你的幫助。我需要輸出3列3年的日曆。在第一列中,年份應該是用戶輸入的年份。2列中的Java控制檯日曆

我有一個代碼,將發佈3年的日曆,但只在1列。我需要在另一列分開另一年plss幫我

package project; 

import java.util.*; 

public class les1 { 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     Calendar cal = Calendar.getInstance(); 
     String ans; 
     // getting the system calendar's year, maximum days, 
     // weeks per month and system calendar's month. 
     int year = cal.get(Calendar.YEAR); 
     int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
     int week = cal.get(Calendar.DAY_OF_WEEK); 
     int month = cal.get(Calendar.MONTH); 
     int col[] = new int[3]; 

     do { 
      // validation for input year 

      do { 
       System.out.print("\nEnter year: "); 
       while (true) 
        try { 
         year = Integer.parseInt(in.next()); 
         break; 
        } catch (Exception ex) { 
         System.out.println("Try again!"); 
         System.out.print("\nEnter year: "); 
        } 
       if (year < 1900 || year > 2099) { 
        System.out.println("Try again!"); 
       } 
      } while (year < 1900 || year > 2099); 

      /* 
      * System.out.println("enter number of elements"); 
      * 
      * int n = in.nextInt(); 
      * 
      * int arr[] = new int[n]; 
      * 
      * for (int j = 0; j < n; j++) {// for reading array 
      * 
      * } 
      * 
      * for (int j : arr) { 
      */ 
      // loop for months 
       for (month = 1; month <= 1; month++) { 
        // calculating years, month, days and weeks. 
        int y = year - (14 - month)/12; 
        int x = y + y/4 - y/100 + y/400; 
        int m = month + 12 * ((14 - month)/12) - 2; 
        int d = (1 + x + (31 * m)/12) % 7; 
        // calculate leap year 
        boolean LeapYear = (year % 400 == 0); 
        // displaying month as String 
        cal.set(year, month, 0); 
         System.out 
           .print(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "\t\t\t\t\t\t"); 
         System.out.print("Sun Mon Tue Wed Thu Fri Sat" + "\t\t\t"); 

        int length = (int) (30 + ((month + (month/8.0)) % 2)); 
        if (month == 2) { 
         if (LeapYear) { 
          length -= 1; 
         } else { 
          length -= 2; 
         } 
        } 

        for (int a = 0; a < col.length; a++) { 
         int counter = 1; 
         // spacing for first day of the month 
         for (int i = 0; i < d; i++) { 
          System.out.print(" "); 
          counter++; 
         } 
         // spacing for days 
         for (day = 1; day <= length; day++) { 
          System.out.printf("%2d", day); 
          System.out.print((counter++ % 7 != 0) ? " " : "\n"); 
         } 
        } 
        week = (week + length) % 6; 
       } 
      year++; 
      /* } */ 
      System.out.print("\n\n" + "Enter another year (Y/N)? "); 
      ans = in.next(); 
     } while (ans.equals("Y") || ans.equals("y")); 
     System.out.println("\nEnd!"); 
     in.close(); 
    } 
} 
+0

3年的日曆是什麼意思?請詳細解釋每個列的預期輸出是什麼,以及輸出結果如何? – sifho

+0

例如,我將輸入2015,然後控制檯將顯示2015年的第1列中的日曆,然後是2016年的第2列中的日曆,然後是第3列中的2017年中的日曆。 –

回答

0

看看下面的代碼。它將打印三欄不同年份的日曆。我已使用this公式來確定任何月份的一週中的哪一天。

import java.util.*; 

public class les1 { 

public static void main(String[] args) { 

    String[] days = { 
     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 
    }; 
    Scanner in = new Scanner(System. in); 
    Calendar cal = Calendar.getInstance(); 
    String ans; 
    int year = cal.get(Calendar.YEAR); 
    int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
    int week = cal.get(Calendar.DAY_OF_WEEK); 
    int month = cal.get(Calendar.MONTH); 
    int col[] = new int[3]; 

    do { 
     // validation for input year 

     do { 
      System.out.print("\nEnter year: "); 
      while (true) 
      try { 
       year = Integer.parseInt(in .next()); 
       break; 
      } catch (Exception ex) { 
       System.out.println("Try again!"); 
       System.out.print("\nEnter year: "); 
      } 
      if (year < 1900 || year > 2099) { 
       System.out.println("Try again!"); 
      } 
     } while (year < 1900 || year > 2099); 

     for (month = 1; month <= 1; month++) { 


      HashMap < Integer, Integer > map = new HashMap < Integer, Integer >(); 
      map.put(1, 0); 
      map.put(7, 6); 
      map.put(2, 3); 
      map.put(8, 2); 
      map.put(3, 3); 
      map.put(9, 5); 
      map.put(4, 6); 
      map.put(10, 0); 
      map.put(5, 1); 
      map.put(11, 3); 
      map.put(6, 4); 
      map.put(12, 5); 

      System.out.println(); 

      for (int a = 0; a < col.length; a++) { 
       int counter = 1; 
       System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); 
       System.out.printf("\t\t\t"); //Formatting for days 

      } 
      // System.out.println(); 

      // calculate leap year 
      boolean LeapYear = false; 
      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true; 
      // displaying month as String 

      ArrayList <Integer> dds = new ArrayList <Integer>(); 
      dds.add(0); 
      dds.add(0); 
      dds.add(0); 
      dds.add(0); 
      int indexofA = 0; 

      String yy = year + ""; 

      int m; 
      if (LeapYear && month == 1) m = map.get(month) + 6; 
      else if (LeapYear && month == 2) m = map.get(month) - 1; 
      else m = map.get(month); 

      int y = Integer.parseInt(yy.substring(2, 4)); 
      int c = (year/100); 
      if (c % 4 == 0) c = 6; 
      else if (c % 4 == 1) c = 4; 
      else if (c % 4 == 2) c = 2; 
      else c = 0; 
      int x = y + y/4 + c; 
      int d = (1 + x + m) % 7; 
      // cal.set(year, month, 0); 

      int length = (int)(30 + ((month + (month/8.0)) % 2)); 
      if (month == 2) { 
       if (LeapYear) { 
        length -= 1; 
       } else { 
        length -= 2; 
       } 
      } 
      System.out.print("len : " + length); 
      System.out.println(); 
      boolean thirtyone = false; 
      int count = 0; 
      for (int r = 0; r < 6; r++) { //For each row of the calendars 

       String[] dayss = new String[7]; 

       int index = 0; 

       for (int a = 0; a < col.length; a++) { //For each column of a calendar 

        for (int i = 0; i < d && dds.get(indexofA) == 0; i++) { 

         dayss[index] = " "; 
         index++; 
        } 

        for (day = dds.get(indexofA) + 1; day <= length; day++) { 
         if (day == 31) { 
          dayss[index] = day + ""; 
          index++; 
          thirtyone = true; 

         } else if (day < 31) { 
          dayss[index] = day + ""; 
          index++; 
         } 

         if (index == 7) { 
          dds.set(indexofA, day); 
          break; 
         } 

        } 

        indexofA++; 
        System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", dayss[0], dayss[1], dayss[2], dayss[3], 
        dayss[4], dayss[5], dayss[6]); 
        System.out.printf("\t\t\t"); 

        index = 0; 
        year++; 
        LeapYear = false; 
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true; 
        yy = year + ""; 
        if (LeapYear && month == 1) m = map.get(month) + 6; 
        else if (LeapYear && month == 2) m = map.get(month) - 1; 
        else m = map.get(month); 

        y = Integer.parseInt(yy.substring(2, 4)); 
        c = (year/100); 
        if (c % 4 == 0) c = 6; 
        else if (c % 4 == 1) c = 4; 
        else if (c % 4 == 2) c = 2; 
        else c = 0; 
        x = y + y/4 + c; 
        d = (1 + x + m) % 7; 

       } 
       year -= 3; 
       yy = year + ""; 
       LeapYear = false; 
       if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true; 

       if (LeapYear && month == 1) m = map.get(month) + 6; 
       else if (LeapYear && month == 2) m = map.get(month) - 1; 
       else m = map.get(month); 

       y = Integer.parseInt(yy.substring(2, 4)); 
       c = (year/100); 
       if (c % 4 == 0) c = 6; 
       else if (c % 4 == 1) c = 4; 
       else if (c % 4 == 2) c = 2; 
       else c = 0; 
       x = y + y/4 + c; 
       d = (1 + x + m) % 7; 

       indexofA = 0; 
       System.out.println(); 

      } 

     } 
     System.out.print("\n\n" + "Enter another year (Y/N)? "); 
     ans = in .next(); 
    } while (ans.equals("Y") || ans.equals("y")); 
    System.out.println("\nEnd!"); in .close(); 
} 

}