如何使用基于用户输入显示日期的数组编写程序? 示例输入是1和 输出将是它的星期日
答案 0 :(得分:0)
public static void main(String [] args){
int day;
Scanner in = new Scanner(System.in);
String days[] = { "", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
boolean finish = false;
while (!finish)
{
System.out.println("Please enter a day: ");
day = in.nextInt();
for (int i = 1; i <= 8; i++) {
if (day==i)
{
System.out.println("Its " + (days[i]));
finish = true;
break;
}
}
if (!finish)
System.out.println("Can't find " + day
+ " in the array, please try again!");
}
}
}