日期:2014-05-20 浏览次数:20883 次
public static void main(final String[] args) {
final String[] weekdays ={"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入数字1-7(输入0结束):");
int x = scanner.nextInt();
if (x == 0) break;
if (x > 7 || x < 0) continue;
System.out.printf("今天是 %s%n", weekdays[x-1]);
}
}
------解决方案--------------------
public class TestOutput{
public static void main(String[] args) {
String [] days = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字1-7(输入0结束)");
while (scanner.hasNext()) {
try {
int temp = scanner.nextInt();
if (temp == 0)
break;
if(temp<0||temp>7)
throw new Exception("输入错误");
System.out.println("今天是"+days[temp-1]);
System.out.print("请输入数字1-7(输入0结束)");
} catch (Exception e) {
try {
throw new Exception("输入错误");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
System.out.print("程序结束!");
}
}