import java.util.Scanner;
public class Test {
public void testDay() {
Scanner scn = new Scanner(System.in); // 要装JDK5.0以上才能支持
try {
while (true) {
System.out.print("请输入1-12中的一个数:");
int input = scn.nextInt();
switch (input) {
case 1,3,5,7,8,10,12: //大月31天
System.out.println("该月对应天数为:" + 31 + "天");
return;
case 4,6,9,11: //小月30天
System.out.println("该月对应天数为:" + 30 + "天");
return;
case 2: //没输入年所以显示28、29都可以把 @)@
System.out.println("该月对应天数为:" + 29 + "天");
return;
default:
System.out.println("输入数字不在1-12范围内!");
break;
}
}
} catch (RuntimeException e) {
System.out.println("小样,不要欺负我没文化!请重新输入!");
testDay();
}
}
public static void main(String[] args) {
Test test = new Test();
test.testDay();
}
}
参考资料:对1楼答案进行了一点优化