Java语言程序设计(基础篇)第十版 综合题 **3.21(科学:某天是星期几)

本文介绍了一个Java程序,用于计算输入的年月日对应的是星期几。通过用户输入的日期信息,程序采用特定算法计算出该天是一周中的哪一天,并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


public class ComputeWeek {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        java.util.Scanner input = new java.util.Scanner(System.in);

        System.out.print("Enter year: (e.g.,2012): ");
        int y = input.nextInt();
        System.out.print("Enter month: 1-12: ");
        int m = input.nextInt();
        System.out.print("Enter the day of the month : 1-31: ");
        int q = input.nextInt();


        int j = (int)(y /100);  //世纪数 
        int k =  y %100; //该世纪的第几年

        int h =  (q    + (26*(m+1)/10) + k + (k / 4) + (j / 4)  + (5*j))%7;

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


    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值