第三章第二十一题(科学:某天是星期几)(Science: day of the week)

第三章第二十一题(科学:某天是星期几)(Science: day of the week)

  • **3.21(科学:某天是星期几)泽勒一致性是由克里斯汀\LARGE \cdot泽勒开发的用于计算某天是星期几的算法。这个公式是:
    h = (q+(26(m+1)/10+k+k/4+j/4+5/j))%7

    其中:

    h是一个星期中的某一天(0为星期六;1为星期天;2为星期一;3为星期二;4为星期三;5为星期四;6为星期五)。
    q是某月的第几天。
    m是月份(3为三月,4为四月,……,12为十二月)。一月和二月分别记为上一年的13和14月。
    j是year/100。
    k是该世纪的第几年(即year%100)。
    注意:公式中的除法执行一个整数相除。编写程序,提示用户输入年、月、和该月的哪一天,然后显示它是一周中的星期几。

    下面是一些运行示例:

    Enter year:(e.g.,2012): 2015

    Enter month:1-12: 1

    Enter the day of the month: 1-31: 25

    Day of the week is Sunday

    Enter year:(e.g.,2012): 2012

    Enter month:1-12: 5

    Enter the day of the month: 1-31: 12

    Day of the week is Saturday

    **3.21(Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is
    h = (q+(26(m+1)/10+k+k/4+j/4+5/j))%7

    where
    h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, and 6: Friday).
    q is the day of the month.
    m is the month (3: March, 4: April, . . ., 12: December). January and February are counted as months 13 and 14 of the previous year.
    j is year / 100
    k is the year of the century (i.e., year % 100).
    Note all divisions in this exercise perform an integer division. 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.

    Here are some sample runs:
    Enter year:(e.g.,2012): 2015

    Enter month:1-12: 1

    Enter the day of the month: 1-31: 25

    Day of the week is Sunday

    Enter year:(e.g.,2012): 2012

    Enter month:1-12: 5

    Enter the day of the month: 1-31: 12

    Day of the week is Saturday

  • 参考代码:

package chapter03;

import java.util.Scanner;

public class Code_21 {
    public static void main(String[] args) {
        int year, month, day, h, q, m, j, k;

        // Prompt the user to enter the year,the month and the day
        System.out.print("Enter year: (e.g.,2012):");
        Scanner input = new Scanner(System.in);
        year = input.nextInt();

        System.out.print("Enter month: 1-12:");
        month = input.nextInt();

        System.out.print("Enter the day of the month: 1-31:");
        day = input.nextInt();

        q = day;
        if(month == 1 || month == 2) {
            month += 12;
            year -= 1;
        }
        m = month;
        j = year / 100;
        k = year % 100;

        // Calculate the day of the week
        h = (q + (26 * (m + 1) / 10) + k + k / 4 + j / 4 + 5 * j) % 7;

        // Display the day of the week
        switch(h) {
            case 0:
                System.out.println("Day of the week is Saturday");
                break;
            case 1:
                System.out.println("Day of the week is Sunday");
                break;
            case 2:
                System.out.println("Day of the week is Monday");
                break;
            case 3:
                System.out.println("Day of the week is Tuesday");
                break;
            case 4:
                System.out.println("Day of the week is Wednesday");
                break;
            case 5:
                System.out.println("Day of the week is Thursday");
                break;
            case 6:
                System.out.println("Day of the week is Friday");
                break;
        }

        input.close();
    }
}

  • 结果显示:
Enter year: (e.g.,2012):2015
Enter month: 1-12:1
Enter the day of the month: 1-31:25
Day of the week is Sunday

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值