HW5.34

时间:2021-12-06 07:43:17

HW5.34

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the year: ");
         int year = input.nextInt();
         input.close();

         for(int i = 1; i <= 12; i++)
             System.out.println(year + "-" + getMonth(i) + "-" + "1 is " + getWeekday(year, i, 1));
     }

     public static String getMonth(int monthValue)
     {
         String monthStr = "";

         switch(monthValue)
         {
             case 1: monthStr = "January"; break;
             case 2: monthStr = "February"; break;
             case 3: monthStr = "March"; break;
             case 4: monthStr = "April"; break;
             case 5: monthStr = "May"; break;
             case 6: monthStr = "June"; break;
             case 7: monthStr = "July"; break;
             case 8: monthStr = "August"; break;
             case 9: monthStr = "Sepetember"; break;
             case 10: monthStr = "October"; break;
             case 11: monthStr = "November"; break;
             case 12: monthStr = "December"; break;
         }

         return monthStr;
     }

     public static String getWeekday(int year, int month, int day)
     {
         if(month == 1)
         {
             month = 13;
             year--;
         }

         if(month == 2)
         {
             month = 14;
             year--;
         }

         int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
         String weekStr = "";

         switch(week)
         {
             case 0: weekStr = "Monday"; break;
             case 1: weekStr = "Tuesday"; break;
             case 2: weekStr = "Wednesday"; break;
             case 3: weekStr = "Thursday"; break;
             case 4: weekStr = "Friday"; break;
             case 5: weekStr = "Saturday"; break;
             case 6: weekStr = "Sunday"; break;
         }
         return weekStr;
     }
 }

相关文章