分数判断

编写一个程序,此程序在运行时要求用户输入一个    整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什    么样的内容,都不会崩溃。

import java.util.Scanner;
/***
 * 
 * @author 吕鹏博
 *2016年11月25日 21:15:13
 */
class MyException extends Exception{
    public MyException(String msg){
        super(msg);
    }
}
public class GradeJudgement {

    
    public static void main(String[] args) throws MyException{
        String message="";
        double value=0;
        System.out.println("请输入需要判断的成绩");
        Scanner s=new Scanner(System.in);
        try{
        message=s.nextLine();
        if(!isNum(message)){
            MyException s1=new MyException("输入非数字,请重新输入。");
            throw s1;
        }
        try{
            value=Double.parseDouble(message);
            if(value<0){
                MyException s2=new MyException("输入数值小于零,请重新输入。");
                throw s2;
            }}
        catch(MyException s2){System.out.println(s2);System.exit(0);}
        }
        catch(MyException s1){
            System.out.println(s1);
            System.exit(0);
        }
        finally{
            if(value<60){
            System.out.println("该门课程成绩不及格。");
            }
            else if(value<70){
                System.out.println("该门课程成绩及格。");
            }
            else if(value<80){
                System.out.println("该门课程成绩中等。");
            }
            else if(value<90){
                System.out.println("该门课程成绩良好。");
            }
            else if(value<100){
                System.out.println("该门课程成绩优秀。");
            }
            else{
                System.out.println("输入值非法,无法判断。");
            }
        }

    }
    public static boolean isNum(String str){
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }

}

 

posted @ 2016-11-25 21:51  L.P.B_Blizzard  阅读(220)  评论(0编辑  收藏  举报