问答题

【说明】本程序实现功能:读入两个整数,第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
DivideByZeroEx......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】本程序将两个从小到大的有序链表合成一个新的从小到大的有序链表。链表的每一项由类Node描述,而链表由类List描述。类List的成员函数有以下几个。 ①createList():创建从小到大的有序链表。 ②multiplyList(List L1,List L2):将链表L1和链表L2合并。 ③print();打印链表。 # include <iostream.h>class List;class Node friend class List; public: Node(int data) (1) ; private: int data; Node *next; ;class List public: List( ) list = NULL; void multiplyList(List L1, List L2); void createList( ); void print( ); private: Node *list;;void List::createList( ) Node *p, *u, *pm; int data; list = NULL; while (1) cout<< 输入链表的一项: (小于零,结束链表) <<end1; cin >> data; if(data<0)break; 小于零,结束输入 p = list; while (p != NULL && data > p->data) 查找插入点 pre = p; p = p->next; u= (2) : if(p==list) list = u; else pre->next = u; (3) :void List::multiplyList (List L1, List L2) Node *pL1, *pL2, *pL, *u; list = NULL; pL1 = L1.list; pL2 = L2.1ist; while (pL1 != NULL && pL2!= NULL) if (pL1->data < pL2->data) u = new Node (pL1->data); pL1 = pL1 ->next; else u = new Node (pL2->data)); pL2 = pL2->next; if (list==NULL) list= (4) ; else pL->next = u; pL =u; pL1 = (pL1 != NULL) pL1:pL2; while (pL1 != NULL) u = (5) ; pL1 = pL1->next; if (list==NULL) list=pL=u; else pL->next = u; pL = u; void List::print( ) Node *p; p = list; while (p != NULL) cout << p->data << t ; p = p->next; cout << end1;void main ( ) List L1, L2, L; cout << 创建第一个链表 n ; L1.createList ( ); cout << 创建第二个链表 n ; L2.createList ( ); L1.print ( ); L2.print ( ); L.multiplyList (L1, L2); L.print ( );