第三章第五题(给出将来的日期)(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