7-9 sdut -JAVA-Day Of Week
分数 9
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
Zeller's congruence is an algorithm devised by Christian Zeller to calculate the day of the week for a given date. The formula is as follow:
(Note: Division in this formula is integer division) where
• q is the day of the month (1 = first day of month, 2 = second day of month, ...)
• m is the month (3 = March, 4 = April, 5 = May, 6 = June, 7 = July, 8 = August, 9 = September, 10 = October, 11 = November, 12 = December, 13 = January, 14 = February)
• j is the century part of year e.g. for year 2013, century part is 20
• k is the year of the century e.g. for year 2013, year of the century is 13
Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week.
If the month value supplied by the end user is outside the range 1 to 12 your program should display an appropriate error message and then end. Note: January and February are counted as 13 and 14 in the formula. You will need to convert the user input and also change the year to the previous year if the user enters a value of 1 or 2 for the month.
If the day value supplied by the end user is outside the range of 1 to 31 your program should display an appropriate error message and then end. You are not expected to check if the values supplied by the end user constitute a valid date. The values supplied by the end user in Figure 3 do not constitute a valid date, yet the program produces an answer. This is acceptable in answer to this question at this point in time.
Input Specification:
enter a year, month, and day of the month.
Output Specification:
displays the name of the day of the week.
Sample Input:
2023 03 30
Sample Output:
The corresponding day of the week is:Thursday
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int[] b = new int[4]; for(int i = 3;i>=0;i--){ b[i] = year%10; year /= 10; } int j = b[0]*10 + b[1]; int k = b[2]*10 + b[3]; int month = sc.nextInt(); if(month<1 || month > 10){ System.out.println(""); System.out.println("Month must be in the range 1 to 12."); System.exit(0); } if(month == 1){ month = 13; } if(month == 2){ month = 14; } int day = sc.nextInt(); if(day < 1 || day > 31){ System.out.println(""); System.out.println("Day must be in the range 1 to 31"); System.exit(0); } int h = (day + (26*(month+1))/10+k+k/4+j/4+5*j)%7; switch (h-1) { case 1: System.out.println("The corresponding day of the week is:Monday"); break; case 2: System.out.println("The corresponding day of the week is:Tuesday"); break; case 3: System.out.println("The corresponding day of the week is:Wednesday"); break; case 4: System.out.println("\nThe corresponding day of the week is:Thursday"); break; case 5: System.out.println("The corresponding day of the week is:Friday"); break; case 6: System.out.println("The corresponding day of the week is:Saturday"); break; case 7: System.out.println("The corresponding day of the week is:Sunday"); break; default: break; } } }