1. 程式人生 > >尚學堂java 答案解析 第六章

尚學堂java 答案解析 第六章

本答案為本人個人編輯,僅供參考,如果讀者發現,請私信本人或在下方評論,提醒本人修改

一.選擇題

1.C

解析:對void下的函式,可以使用"return;"表示結束之意,但不能"return i",因為資料是void,

try-catch-finally:正確執行:try->finaly,除非try裡含有System.exit(0)強制退出.錯誤執行:try(錯誤時跳)->catch->finally,對finally同上理.

2.C

解析:因為一般情況下無論try-catch語句是否異常,finaly語句最後都會執行.

3.AC

解析:throws用於申明異常

,在方法申明時候使用;throw用於丟擲異常,在方法體內部使用.

4.BC

解析:A:Exception是所有異常類的父類,不是執行異常,SexException不存在

B:NullPointerException是執行時發現在不能引用NULL物件的情況下引用NULL物件

InputMismatchException是輸入時發現型別不對

C.ArithmeticException計算時發現的錯誤,最常見的是除0操作

ArrayIndexOutOfBoundsException陣列越界,java在編譯之前是不檢查資料越界問題的.

D.ClassNotFoundException沒有發現相關類,直接在寫程式時候編譯器就檢查

ClassCastException型別轉換異常,常見是下轉型時出錯,同編譯器檢查

5.B

解析:輸入-1時會丟擲自定義異常,結束try-catch執行

二.簡答題

4.throws用於申明異常,在方法申明時候使用;throw用於丟擲異常,在方法體內部使用.;

三.程式設計題

1.

import java.util.Scanner;

public class Throws extends  Exception{
    void gradeException(){
        System.out.printf("分數只能在0-100之間\n");
    }
}
class ch6_1{
    public static void main(String[] args) throws Throws{
        Scanner input = new Scanner(System.in);
        float grade = 0.0f;
       while (true) {
           try{
               System.out.println("請輸入分數:");
               grade = input.nextFloat();
               if(grade > 100 || grade <0)
                   throw new Throws();
           }
           catch (Throws e){
               e.gradeException();
           }
       }

       }
}

2.

import java.util.Scanner;

public  class  IllegalArgumentException extends Exception {
    void IllegalArgumentException(int a,int b, int c){
        System.out.printf("\n%d,%d,%d不能構成三角形",a,b,c);
    }

}

 class isTriangle {
     void isTriangle(int a, int b, int c) throws IllegalArgumentException {
         int max = Math.max(Math.max(a, b), c);
         int min = Math.min(Math.min(a, b), c);
         int sum = a + b + c;
         int twoEdgeDec = sum - max - min - min;     //較小的兩邊之差
         int twoEdgeAdd = sum - max;                //較小的兩邊之和


         //兩邊之和小於第三邊,兩邊之差大於第三邊
         if (max >= twoEdgeAdd || min <= twoEdgeDec)
             throw new IllegalArgumentException();
         else System.out.printf("可以構成三角形");
     }

 }



class ch6_2{
    public static void main(String[] args) {
        int a[] = new int[3];
        Scanner input = new Scanner(System.in);

        System.out.print("請輸入三角形三邊:");
        for(int i = 0;i < 3; i++){
            a[i] = input.nextInt();
        }

        System.out.print("三邊:");
        for(int i : a){
            System.out.printf("\t%d",i);
        }

        isTriangle isTriangle = new isTriangle();


        try{
            isTriangle.isTriangle(a[0],a[1],a[2]);
        }
        catch (IllegalArgumentException e){
            e.IllegalArgumentException(a[0],a[1],a[2]);
        }


    }
}

3.

import java.util.Scanner;

public class aver {
}

class Throws extends  Exception{
    void gradeException(){
        System.out.printf("分數必須是正數或者0\n");
    }
}
class ch6_1{
    public static void main(String[] args) throws Throws{
        Scanner input = new Scanner(System.in);
        float grade = 0.0f;
        float aver = 0.0f;
        float sum = 0.0f;
        int stuNum = 0;


        System.out.print("請輸入學生人數:");
        stuNum = input.nextInt();
       for(int i = 0 ; i < stuNum ; i++ ){
           try{

               System.out.println("請輸入學生分數:");
               grade = input.nextFloat();
               if(grade > 100 || grade <0)
               {
                   i--;
                   throw new Throws();

               }
               else {
                   sum  = sum + grade;
                   aver = sum/stuNum;
                   System.out.printf("總分數:%3.2f \t 平均分:%3.2f \n",sum,aver);

               }

           }
           catch (Throws e){
               e.gradeException();
           }
       }



    }
}