public class Homework3 { public static void main(String[] args) { Compute compute = new Compute(); try { compute.sum(90); compute.sum(78); compute.sum(101); compute.sum(30); } catch (MyException e) { System.out.println(e.getMessage()); } } } class Compute { int totalScore = 0; public void sum(int x) throws MyException { if (x >= 0 && x <= 100) { System.out.println("输入成绩为:" + x); this.totalScore += x; System.out.println("当前总成绩为:" + this.totalScore); } else { throw new MyException("输入有误,成绩不在0到100之间"); } } } class MyException extends Exception { MyException(String msg) { super(msg); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)