Topics requirements: Input value Output 90-100 "excellent", 80-90 outputs "good", 70-80 Output "", 60-70 outputs "pass", 0-60 output "fail", the other input capture exceptions and prompts the appropriate information.
code show as below:
1 import java.util.Scanner; 2 3 @SuppressWarnings("serial") 4 //自定义异常类 5 class TryException extends Exception 6 { 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 public TryException(String Message) { 12 super(Message); 13 } 14 } 15 16 public class{TryTheWrong . 17 static Scanner in = new new Scanner (the System.in); 18 is public static void main (String args []) { . 19 // run the catch-the try 20 is the try { 21 is JudgeError (); 22 is } 23 is the catch (TryException E) { 24 // output custom exception information 25 System.out.println (e.getMessage ()); 26 is } 27 } 28 // operating method, comprising an input value, and the determination of the value of the abnormality information to be fed back to the throw a custom exception class 29 public static voidJudgeError () throws TryException 30 { 31 is String S = in.next (); 32 String m = "^ [0-9] * $" ; 33 is // determines whether all numbers 34 is IF (! S.matches (m) ) { 35 the throw new new TryException ( "input-containing non-numeric entry" ); 36 } 37 [ // String transfected int 38 is int I = Integer.valueOf (S); 39 IF (I> 100 ) { 40 the throw new new TryException ( "input value is less than 0 or greater than 100 " ); 41 } 42 if(i>=90&&i<=100) { 43 System.out.println("优"); 44 } 45 else if(i>=80&&i<90) { 46 System.out.println("良"); 47 } 48 else if(i>=70&&i<80) { 49 System.out.println("中"); 50 } 51 else if(i>=60&&i<70) { 52 System.out.println("及格"); 53 } 54 else if(i>=0&&i<60) { 55 System.out.println("不及格"); 56 } 57 else { 58 System.out.println("ERROR"); 59 } 60 } 61 }
Create custom exception class TryException, using super () to capture and throw new abnormality information feedback, and finally through the catch System.out.println (e.getMessage ()); output. Note that in applying this operation, class (JudgeError) throws corresponding to the class name (TryException). Since the input is unknown, it may contain non-numeric elements, so when performing the input operation, as the first character string input by a regular expression determined whether all numbers, then the string is converted to int, determines whether it is greater than 100, according to the result determined whether feedback exception information, if there is no problem, continue with the following if-else if operation, output the correct result.