尚学堂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运行

二.简答题

1.https://www.cnblogs.com/lcl-dcr/p/7653274.html

2.https://blog.csdn.net/qq_18505715/article/details/76696439

3.https://blog.csdn.net/uniquewonderq/article/details/46426383

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();
           }
       }



    }
}

猜你喜欢

转载自blog.csdn.net/qq_34834846/article/details/81534034