Ø编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
Ø要求程序必须具备足够的健壮性,不管用户输入什么样的内容,都不会崩溃。
【源代码】
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner con=new Scanner(System.in);
try {
System.out.println("请输入成绩:");
int ch=con.nextInt();
if(ch<60&&ch>=0) {System.out.println("不及格!");}
else if(ch==60) {System.out.println("及格!");}
else if(ch>60&&ch<80) {System.out.println("中!");}
else if(ch>=80&&ch<90) {System.out.println("良!");}
else if(ch>=90&&ch<=100){System.out.println("优!");}
}catch(ArrayStoreException e){
e.printStackTrace();
}finally {
System.out.println("Over");
System.exit(0);
}
}
}
【运行截图】
错误分析:
用 “存入数组的内容数据类型不一致所产生的异常” 的异常类来获取异常。