我制作了一个程序,要求用户输入月份的编号,程序会告诉用户月份的名称和季节。我使用catch尝试,因此如果用户输入了错误的值,程序将告诉它是错误的,并重复循环直到用户输入正确的值。如果用户输入整数,则程序运行良好,但是如果他输入其他内容,则程序将在循环中运行而不会停止!我尝试使用break,但可以使用,但循环不起作用,即不会要求用户再次输入数字。这是程序;请帮我修复它。
package Course;
// this program demonstrate the Switch
import java.util.Scanner;
public class SimpleSwitch {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String month="Null";
String Season="Null";
int i=0;
System.out.println("Pleae Enter Month Number: ");
while ( i<=0|| i>=13 ) {
try{
i= sc.nextInt();
switch(i){
case 1:
month = "January";
Season = "Winter";
break;
case 2:
month = "Febrary";
Season = "Winter";
break;
case 3:
month = "March";
Season = "Spring";
break;
case 4:
month = "April";
Season = "Spring";
break;
case 5:
month = "May";
Season = "Spring";
break;
case 6:
month = "June";
Season = "Summer";
break;
case 7:
month = "July";
Season = "Summer";
break;
case 8:
month = "August";
Season = "Summer";
break;
case 9:
month = "Septemper";
Season = "Fall";
break;
case 10:
month = "October";
Season = "Fall";
break;
case 11:
month = "November";
Season = "Fall";
break;
case 12:
month = "December";
Season = "Winter";
break;
default:
System.out.println("There is no month Number "+ i+ ", Please Enter the correct Month number: ");
}
}catch(RuntimeException ex){
System.out.println("Invalid input");
i=0;
}
}
System.out.println("Month number "+ i+ " is "+ month+ " and it is in "+ Season);
}
}