第三章第五题(给出将来的日期)(Find future dates)

第三章第五题(给出将来的日期)(Find future dates)

  • *3.5(给出将来的日期)编写一个程序,提示用户输入代表今天日期的数字(周日为0,周一为1,……,周六为6)。同时,提示用户输入一个今天之后的天数,作为代表将来某天的数字,然后显示这天是星期几。
    下面是一个运行示例:
    Enter today’s day:1
    Enter the number of days elapsed since today:3
    Today is Monday and the future day is Thursday
    Enter today’s day:0
    Enter the number of days elapsed since today:31
    Today is Sunday and the future day is Wednesday

    *3.5(Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, . . . , and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week.
    Here is a sample run:
    Enter today’s day:1
    Enter the number of days elapsed since today:3
    Today is Monday and the future day is Thursday
    Enter today’s day:0
    Enter the number of days elapsed since today:31
    Today is Sunday and the future day is Wednesday

  • 参考代码:

package chapter03;

import java.util.Scanner;

public class Code_05 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter today's day: ");
        int number1 = input.nextInt();
        System.out.print("Enter the number of days elapsed since today: ");
        int number2 = input.nextInt();
        System.out.print("Today is ");
        method(number1);
        System.out.print(" and the future day is ");
        method((number2 + number1) % 7);
    }
    static void method(int n){
        switch (n){
            case 0:System.out.print("Sunday");break;
            case 1:System.out.print("Monday");break;
            case 2:System.out.print("Tuesday");break;
            case 3:System.out.print("Wednesday");break;
            case 4:System.out.print("Thursday");break;
            case 5:System.out.print("Friday");break;
            case 6:System.out.print("Saturday");break;
        }
    }
}

  • 结果显示:
Enter today's day: 1
Enter the number of days elapsed since today: 3
Today is Monday and the future day is Thursday
Process finished with exit code 0

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值