如果输入非法数字,就结束循环
import java.util.Scanner;
class SeasonDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("请输入月份(1-12):");
int month = sc.nextInt();
boolean onOff = true; //控制while语句
while(onOff)
{
switch (month)
{
case 1:
case 2:
case 12:
System.out.println(month+"月份是冬季");
break;
case 3:
case 4:
case 5:
System.out.println(month+"月份是春季");
break;
case 6:
case 7:
case 8:
System.out.println(month+"月份是夏季");
break;
case 9:
case 10:
case 11:
System.out.println(month+"月份是秋季");
break;
default:
System.out.println("您输入的数字非法");
break;
}
System.out.println();
System.out.print("请输入月份(1-12):");
month = sc.nextInt();
if (month <=12 && month > 0) //对键盘录入的数进行比较判断
{
onOff = true;
}
else
{
onOff = false;
System.out.println("您输入的数字非法");
}
}
}
}
|
|