java程序——输入判断成绩

import java.util.*;

class ExceptionOut extends Exception{
    
}
public class Score {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String input,output;
        input = "请输入成绩:";
        System.out.println(input);
        try{
        Scanner sca = new Scanner(System.in);
        int score = sca.nextInt();
        if(score<60&&score>=0)
            output = "不及格!";
        else if(score>=60&&score<70)
            output = "及格!";
        else if(score>=70&&score<80)
            output = "中!";
        else if(score>=80&&score<90)
            output = "良!";
        else if(score>=90&&score<=100)
            output = "优!";
        else
            throw new ExceptionOut();
        }
        catch(ExceptionOut e){
            output = "成绩输入有误!";
            
        }
        catch(Exception e){
            output = "输入格式有误!";
        }

        System.out.println(output);
        
    }

}
结果截图:





 分析:先输入一个整型,然后判断优良,自定义一个ExceptionOut函数继承Exception父类,实现整型超出范围的错误抛出,然后用两个catch来接错误,一个是整型的范围不对,一个是输入的格式不对。

原文地址:https://www.cnblogs.com/xiaosongbiog/p/4960996.html