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

 

 

 


目录
相关文章
|
8月前
|
Java
SDUT java lab 7.3
SDUT java lab 7.3
27 0
|
8月前
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
63 1
|
8月前
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
40 0
|
8月前
|
人工智能 Java
sdut java lab5
sdut java lab5
41 0
|
8月前
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
51 0
|
8月前
|
存储 Java
SDUT java lab7.4
SDUT java lab7.4
42 0
|
8月前
|
Java
SDUT Java lab6
SDUT Java lab6
32 0
|
8月前
|
Java
sdut java lab7单选
sdut java lab7单选
49 0
|
8月前
|
存储 Java 索引
sdut java lab 7.6
sdut java lab 7.6
50 0
|
7天前
|
Java
Java—多线程实现生产消费者
本文介绍了多线程实现生产消费者模式的三个版本。Version1包含四个类:`Producer`(生产者)、`Consumer`(消费者)、`Resource`(公共资源)和`TestMain`(测试类)。通过`synchronized`和`wait/notify`机制控制线程同步,但存在多个生产者或消费者时可能出现多次生产和消费的问题。 Version2将`if`改为`while`,解决了多次生产和消费的问题,但仍可能因`notify()`随机唤醒线程而导致死锁。因此,引入了`notifyAll()`来唤醒所有等待线程,但这会带来性能问题。
Java—多线程实现生产消费者