Tenth JAVA jobs [1] (rain classwork)

topic:

Enter the number 5, on behalf of student achievement, we calculate the average. When the input value is negative or greater than 100, custom exception handling prompt.

 

Code:

 

. 1  / ** 
2  * @author Li Jiaqi
 3  * rain class assignments
 . 4   * / 
. 5  Package the Throwable;
 . 6  
. 7  Import java.util.Scanner;
 . 8  
. 9  class MyException the extends Exception {    // create a custom exception class and the parent class inherits exception 
10      Private  Double Number;
 . 11      MyException ( Double X) {
 12 is          Number = X;
 13 is      }
 14      public String toString () {     // custom exception 
15         return "custom exception [" + number + "score beyond the scope]" ;
 16      }
 . 17  }
 18 is  
. 19  public  class the Test {
 20 is      static  void AVER () throws MyException {    // create test method of averaging in the class declaration and abnormal 
21 is          Double X;
 22 is          Double   SUM = 0 ;
 23 is          Scanner Reader = new new Scanner (the System.in);
 24          System.out.println ( "Please enter the subjects score:" );
 25          for ( int I = 0; I <. 5 ; I ++ ) {
 26 is              X =reader.nextDouble ();
 27              SUM = + X;
 28              IF (X <0 || X> 100 ) {
 29                  the throw  new new MyException (X); // throw a custom exception MyException 
30              }
 31 is          }
 32      System.out.println ( "average score of" + SUM /. 5 );
 33 is      }
 34 is      public  static  void main (String [] args) {
 35          the try {
 36              AVER ();
 37 [          } the catch (MyException Yichang) {         // last captured in the main process abnormal 
38             System.out.println ( "capture" + Yichang);
 39          }
 40      }
 41 is  
42 is }

 

 

 

operation result:

 

Guess you like

Origin www.cnblogs.com/lietian12345/p/11851691.html