# SDUT JAVA lab3.9

7-9 sdut -JAVA-Day Of Week

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;
}

}

}

|
21天前
|
Java
SDUT java lab 7.3
SDUT java lab 7.3
18 0
|
21天前
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
35 1
|
21天前
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
20 0
|
21天前
|

sdut java lab5
sdut java lab5
24 0
|
21天前
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
23 0
|
21天前
|

SDUT java lab7.4
SDUT java lab7.4
20 0
|
21天前
|
Java
SDUT Java lab6
SDUT Java lab6
12 0
|
21天前
|
Java
sdut java lab7单选
sdut java lab7单选
22 0
|
21天前
|

sdut java lab 7.6
sdut java lab 7.6
21 0
|
5天前
|

JAVA-多线程知识点总结（二）
JAVA-多线程知识点总结（二）
10 0