*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
*3.5(给出未来的日期)编写一个程序来提示用户输入一个数字代表今天的日期(Sunday is 0,Monday is 1,...,and Saturday is 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
代码如下:
import java.util.Scanner;
public class Unite3Test5
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("请输入代表今天的整数:");
int num=scan.nextInt();
System.out.println("请输入从今天开始经过的天数:");
int days=scan.nextInt();
switch(num)
{
case 0:
System.out.println("今天是周日");
break;
case 1:
System.out.println("今天是周一");
break;
case 2:
System.out.println("今天是周二");
break;
case 3:
System.out.println("今天是周三");
break;
case 4:
System.out.println("今天是周四");
break;
case 5:
System.out.println("今天是周五");
break;
case 6:
System.out.println("今天是周六");
break;
}
int number=(days+num)%7;
switch(number)
{
case 0:
System.out.println("经过"+days+"后是周日");
break;
case 1:
System.out.println("经过"+days+"后是周一");
break;
case 2:
System.out.println("经过"+days+"后是周二");
break;
case 3:
System.out.println("经过"+days+"后是周三");
break;
case 4:
System.out.println("经过"+days+"后是周四");
break;
case 5:
System.out.println("经过"+days+"后是周五");
break;
case 6:
System.out.println("经过"+days+"后是周六");
break;
}
}
}
结果如下: