日期:2014-05-20 浏览次数:21068 次
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
try {
scan.nextInt();
} catch (RuntimeException e) {
System.out.println("输入的并非全数字.");
}
}
------解决方案--------------------
注意Integer.parseInt和Scanner.nextInt只作整数验证,如需进一步验证可判断输入值:
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
int score=0;
System.out.print("请输入XX的成绩:");
try {
score=input.nextInt();
if(score<0||score>100)
throw new InputMismatchException();
} catch (InputMismatchException ex) {
System.out.println("输入必须为0-100之间的整数!");
System.exit(0);
}
System.out.print("XX的成绩级别为:");
if(score<60)
System.out.println("E");
else if(score<70)
System.out.println("D");
else if(score<80)
System.out.println("C");
else if(score<90)
System.out.println("B");
else
System.out.println("A");
}