只是一个简短的程序,可以计算出用户输入的天数中的哪一天。我从星期六的第 6 天开始。有没有办法可以使用循环来缩短代码?
import java.util.Scanner;
class Week{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int days[] = {1,2,3,4,5,6,7,8};
String dayNames[] = {"Monday","Tuesday","Wednesday","Thursday","Friday",
"Saturday", "Sunday"};
int numdays;
System.out.println("Enter how many days from now: ");
numdays = input.nextInt();
numdays = (6 + numdays) % 7;
if(numdays == 0){
System.out.println(dayNames[6]);
}
else if(numdays == 1 ){
System.out.println(dayNames[0]);
}
else if(numdays == 2){
System.out.println(dayNames[1]);
}
else if(numdays == 3){
System.out.println(dayNames[2]);
}
else if(numdays == 4){
System.out.println(dayNames[3]);
}
else if(numdays == 5){
System.out.println(dayNames[4]);
}
else if(numdays == 6){
System.out.println(dayNames[5]);
}
}
}