网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

下列程序是整数除法计算的程序,要求在出现异常时,能抛出异常信息。

考虑两种异常:

(1)输入非数字除数

(2)输入除法分母为零

该程序运行的三种结果状态如下:

(1)输入两个合法整数

(2)输入非数字除数

(3)输入除数为零

请将程序填写完整。

注意:不改动程序结构,不得增行或删行。

import java.awt.event.*;

public class ex3 extends ______implements ActionListener

{

private JTextField input1,input2, output;

private int number1,number2;

private double result;

public ex3()

{

______("示范异常");

Container c=getContentPane();

c.setLayout(new GridLayout(3,2));

c.add(new JLabe1("输入分子",SwingConstants.RIGHT));

input1=new JTextField(8);

c.add(input1);

c.add(new JLabe1("输入分母和回车",SwingConstants.RIGHT));

input2=new JTextField(8);

c.add(input2);

input2.addActionListener(this);

c.add(new JLabe1(”计算结果”,SwingConstants.RIGHT));

output=new JTextField();

c.add(output);

setSize(400,100);

show();

}

public void actionPerformed(ActionEvent e)

{

DecimalFormat precision3=new DecimalFormat("0.000");

output.setText("");//空的JTextField输出

try{

number1=Integer.parseInt(input1.getText());

number2=Integer.parseInt(input2.getText());

result=quotient(number1,number2);

______;

}

catch (NumberFormatException nfe)

{

______(this,"你必须输入两个整数","非法数字格式",

JOptionPane.ERROR_MESSAGE);

}

catch (Exception dbz)

{

______(this,"除法异常","除数为零",

JOptionPane.ERROR_MESSAGE);

}

}

//定义求商的方法,如遇除数为零时,能抛出异常。

public double quotient(int numerator,int denominator)

throws Exception

{

if(denominator= =0)

throw new Exception();

return(double) numerator/denominator;

}

public static void main(String args[])

{

Java3 app=new Java3();

app.addWindowListener(

new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

e.getWindow().dispose();

System.exit(0);

}

}

);

}

}


参考答案

更多 “ 下列程序是整数除法计算的程序,要求在出现异常时,能抛出异常信息。考虑两种异常:(1)输入非数字除数(2)输入除法分母为零该程序运行的三种结果状态如下:(1)输入两个合法整数(2)输入非数字除数(3)输入除数为零请将程序填写完整。注意:不改动程序结构,不得增行或删行。import java.awt.event.*;public class ex3 extends ______implements ActionListener{private JTextField input1,input2, output;private int number1,number2;private double result;public ex3(){______("示范异常");Container c=getContentPane();c.setLayout(new GridLayout(3,2));c.add(new JLabe1("输入分子",SwingConstants.RIGHT));input1=new JTextField(8);c.add(input1);c.add(new JLabe1("输入分母和回车",SwingConstants.RIGHT));input2=new JTextField(8);c.add(input2);input2.addActionListener(this);c.add(new JLabe1(”计算结果”,SwingConstants.RIGHT));output=new JTextField();c.add(output);setSize(400,100);show();}public void actionPerformed(ActionEvent e){DecimalFormat precision3=new DecimalFormat("0.000");output.setText("");//空的JTextField输出try{number1=Integer.parseInt(input1.getText());number2=Integer.parseInt(input2.getText());result=quotient(number1,number2);______;}catch (NumberFormatException nfe){______(this,"你必须输入两个整数","非法数字格式",JOptionPane.ERROR_MESSAGE);}catch (Exception dbz){______(this,"除法异常","除数为零",JOptionPane.ERROR_MESSAGE);}}//定义求商的方法,如遇除数为零时,能抛出异常。public double quotient(int numerator,int denominator)throws Exception{if(denominator= =0)throw new Exception();return(double) numerator/denominator;}public static void main(String args[]){Java3 app=new Java3();app.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){e.getWindow().dispose();System.exit(0);}});}} ” 相关考题
考题 请用异常处理改造作业1空气质量提醒问题,使其能够接收并处理用户的任何输入。(要求提供程序和截图,比如如果用户输入的不是数字,则给出提示信息“请输入数字”。)

考题 Java的异常是比较独特的,它是程序安全稳定的重要措施。本次作业要求自定义异常并使用异常。 (1)自定义一个异常类,注意其继承自系统的异常类,并要求有构造方法; (2)使用自定义的异常:要求在一个函数中抛出异常,在另一段程序中调用这个函数并捕获异常。 评分标准: 自定义异常类,继承自系统的异常(2分); 异常类有构造方法(2分); 异常类的构造方法中有一个链接内部异常(1分); 有抛出异常(1分); 在抛出异常的方法中有声明throws异常(1分); 有异常捕获和处理(2分); 整体程序比较合理(1分)。

考题 程序中用throw关键字抛出异常时,只能抛出自己定义的异常对象。

考题 1、下列选项中,哪项中的语句用于抛出在程序执行期间出现异常的信号?()A.try语句B.catch语句C.finally语句D.throw语句

考题 6、程序中用throw关键字抛出异常时,只能抛出自己定义的异常对象。

考题 5、程序没有编写代码处理异常时,Java语言的默认异常处理机制是:(1)抛出异常;(2)停止程序的运行。

考题 【单选题】(9-4)以下关于Java应用程序中的异常处理,说法正确的是()。A.Java程序运行过程中一旦出现异常情况,程序运行就终止了。B.在catch子句中匹配异常是一种精确匹配。C.不通过try/catch语句捕获运行时异常,程序能通过编译检查。D.如果在方法申明时利用throws抛出异常A,该方法就必须确实抛出异常A。

考题 程序没有编写代码处理异常时,Java语言的默认异常处理机制是:(1)抛出异常;(2)停止程序的运行。

考题 8、程序中用throw关键字抛出异常时,只能抛出自己定义的异常对象。