SDUT JAVA lab3.9

简介: SDUT JAVA lab3.9

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

 

 

 


目录
相关文章
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
129 1
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
145 0
|
Java
SDUT Java lab6
SDUT Java lab6
103 0
|
Java
sdut java lab7单选
sdut java lab7单选
134 0
|
人工智能 Java
sdut java lab5
sdut java lab5
113 0
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
111 0
|
存储 Java 索引
sdut java lab 7.6
sdut java lab 7.6
179 0
|
存储 Java
SDUT java lab7.4
SDUT java lab7.4
101 0
|
Java
SDUT java lab 7.3
SDUT java lab 7.3
88 0
|
4月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
266 1