标签:row try err int author cat class xtend input
1 package demo2; 2 3 /** 4 * 自定义异常 5 * @author 6 * 7 */ 8 public class MyException extends Exception { 9 10 public MyException() { 11 super(); 12 } 13 14 public MyException(String message) { 15 super(message); 16 } 17 18 }
1 package demo2; 2 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 public class Score { 7 public static void main(String[] args) { 8 9 try { 10 score(); 11 }catch(InputMismatchException e) { 12 System.err.println(e.getMessage()); 13 }catch(MyException e) { 14 System.err.println(e.getMessage()); 15 } 16 System.out.println("程序结束!"); 17 } 18 19 public static void score() throws InputMismatchException,MyException{ 20 Scanner input=new Scanner(System.in); 21 int score=0; 22 System.out.print("请输入成绩:"); 23 //判断输入是否是整数 input.hasNextInt() 24 if(!input.hasNextInt()) { 25 throw new InputMismatchException("输入不能为非数字!"); 26 } 27 28 score = input.nextInt(); 29 if(score<0||score>100) { 30 throw new MyException("请正确输入成绩信息"); 31 } 32 System.out.println("您的成绩是:"+score); 33 34 } 35 }
标签:row try err int author cat class xtend input
原文地址:https://www.cnblogs.com/baichang/p/10056109.html