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:

 

314cbe6c1419c4a1696decea4f0c221f.png

(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.

 

1be776be9748e0390ddc4fd726750125.png

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

}

 

 

 

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值