早教吧 育儿知识 作业答案 考试题库 百科 知识分享
早教吧考试题库频道 --> 计算机类考试 -->软考中级 -->

阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。【说明】本程序实现功能:读入两个整

题目

阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。

【说明】本程序实现功能:读入两个整数,第1个数除以第2个数,声明当除数为零时抛出异常类DivideByZeroException。

public class DivideByZeroException (1) {

public DivideByZeroException ( ) {

super("Attcmpted to divide by zero");

}

}

import java.io. *;

public class Example {

private static int quotient(int numerator, in)\”}t denominator) throws

DivideByZeroException {

if (denominator==0)

throw (2);

return(numerator / denominator);

}

public static void main(String args[]) {

int number1=0, number2=0, result0;

try{

System.out.print1n("Enter the first number:");

number1 = Integer. valueOf(Keyboard.getString()).intValue();

System.out.print1n("Enter the second number:");

number2 = Integer. Va1ueOf(Keyboard.getString()).intValue();

result = quotient(number1,number2);

}

catch (NumberFormatException e) {

System.out.print1n("Invalid integer entered!");

System. exit(-1);

}

catch ((3)) {

System.out.print1n(e.to String());

System.exit(-1);

}

Systcm.out.pfint1n(number1 + "/" + number2 + "=" + result);

}

}

其中, Keyboard类的声明为:

import java.io.*;

public class Keyboard{

static BufferedReader inputStream =(4)

(new InputStreamReader(System.in));

public static int getInteger() {

try(

return (Intoger.valueOf(inputStream.readLine().trim()).intValue());

} catch (Exception e) {

e.printStackTrace();

return 0;

}

}

public static String getString() {

try{

return (inputStream.readLine());

} catch ((5))

{ return "0";}

}

}

参考答案
正确答案:(1)extends ArithmeticException (2)new DivideByZeroExeeption() (3)DivideByZeroException e (4)new BufferedReader (5)IOException e
(1)extends ArithmeticException (2)new DivideByZeroExeeption() (3)DivideByZeroException e (4)new BufferedReader (5)IOException e 解析:(1)extends ArithmeticException
DivideByZeroException类从ArithmeticException类扩展而来。
(2)new DivideByZeroExeeption()
throw一个DivideByZeroException异常函数,打印错误信息。
(3)DivideByZeroException e
捕捉DivideByZeroExeeption异常信息。
(4)new BufferedReader
动态生成一个BufferedReader对象用于输入。
(5)IOException e
字符串成员函数getString中捕捉输入输出异常信息。
看了阅读下列Java程序和程序说明...的网友还看了以下: