日期:2014-05-20 浏览次数:20898 次
Scanner in = new Scanner(System.in);
boolean flag = true;
System.out.print("请输入成绩:");
while (flag)
{
String number = in.next();
if(number.matches("[0-9]*")){
flag = false;
} else{
System.out.println("请输入正确数字类型");
}
}
------解决方案--------------------
boolean flag = true;
Scanner in=new Scanner(System.in);
System.out.print("请输入成绩:");
while(flag){
try{
int score= in.nextInt();
}catch(Exception e){
System.out.print("请输入数字类型!");
in.nextLine();
continue;
}
flag = false;
}
System.out.println("输入正确!");