11th job

I, entitled 1

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

Second, the code

Test.java

/*
 * Create a custom exception class and inherit the parent class exception, create a method of averaging in the test class and unusual statement,
 * Finally catch exceptions in the main method
 */
package d;

import java.util.Scanner;

class MyException extends Exception{
    private double number;
    MyException(double a){
        number=a;
    }
    public String toString () { // custom exception description information 
        return "custom exception [" + number + "out of range of 0-100]" ;
    }
}

public class Test {
    static void aver()throws MyException{
        double a;
        double  sum=0;
        Scanner reader=new Scanner(System.in);
        System.out.println ( "Enter Number 5:" );
         for ( int I = 0; I <5; I ++ ) {
            a=reader.nextDouble();
            SUM + = A;
             IF (A <0 || A> 100 ) {
                 the throw  new new MyException (A); // throw a custom exception MyException 
            }
        }
    System.out.println ( "Mean is:" + SUM /. 5 );
    }
    public static void main(String[] args) {
        try{
            having ();
        } The catch (MyException E) {         // capture a custom exception MyException 
            System.out.println ( "capture" + E);
        }
    }

}

Third, the operating results

 

 

 

I, entitled 2

  Lisa is able to calculate a write three subjects - math, English, program design - grade point average (GPA = total score of three subjects / 3) and intellectual achievements (intellectual achievement = 0.7 * a total score of three subjects) program.

Guess you like

Origin www.cnblogs.com/weiyiren666/p/11848132.html