Java二级考试异常处理综合题解析:整数除法计算器实现
在计算机等级考试二级Java的简单应用题中,异常处理和GUI编程是重要考点。本文将通过一道综合性的整数除法计算器题目,详细解析异常处理机制、Swing组件使用以及程序调试技巧,帮助考生全面掌握这类题型的解答方法。
一、题目分析
题目要求
程序功能:
- 实现整数除法计算GUI程序
处理两种异常:
- 输入非数字(NumberFormatException)
- 除数为零(ArithmeticException)
- 在指定位置补全代码,不能修改已有代码
- 按要求显示三种运行状态的结果
程序结构分析
程序主要包含:
- GUI界面构建(JFrame、JTextField、JLabel)
- 事件处理(ActionListener)
- 异常处理机制(try-catch)
- 业务逻辑(除法运算)
二、解题思路与填空详解
原题目代码
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//*********Found********
public class Java_3 extends ________ implements ActionListener {
private JTextField input1, input2, output;
private int number1, number2;
private double result;
// 初始化
public Java_3()
{
//*********Found********
______( "示范异常" );
Container c = getContentPane();
c.setLayout( new GridLayout( 3, 2 ) );
c.add( new JLabel( "输入分子",
SwingConstants.RIGHT ) );
input1 = new JTextField( 10 );
c.add( input1 );
c.add(
new JLabel( "输入分母和回车",
SwingConstants.RIGHT ) );
input2 = new JTextField( 10 );
c.add( input2 );
input2.addActionListener( this );
c.add( new JLabel( "计算结果", SwingConstants.RIGHT ) );
output = new JTextField();
c.add( output );
setSize( 425, 100 );
show();
}
//处理 GUI 事件
public void actionPerformed( ActionEvent e )
{
DecimalFormat precision3 = new DecimalFormat( "0.000" );
output.setText( "" ); // 空的JTextField输出
//*********Found********
___________ {
number1 = Integer.parseInt( input1.getText() );
number2 = Integer.parseInt( input2.getText() );
result = quotient( number1, number2 );
//*********Found********
output.setText(_______________________________);
}
catch ( NumberFormatException nfe ) {
JOptionPane.showMessageDialog( this,
"你必须输入两个整数",
"非法数字格式",
JOptionPane.ERROR_MESSAGE );
}
catch ( Exception dbze ) {
//*********Found********
_______________________________( 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[] )
{
Java_3 app = new Java_3();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
e.getWindow().dispose();
System.exit( 0 );
}
}
);
}
}
/* JOptionPane类的常用静态方法如下:
showInputDialog()
showConfirmDialog()
showMessageDialog()
showOptionDialog()
*/
第一个填空位置
public class Java_3 extends ________ implements ActionListener {
需要填入:程序的主框架类
正确答案:JFrame
解释:
- 从后续代码可见使用了
getContentPane()
、setSize()
、show()
等方法 - 这些都是JFrame的特性
- 需要继承JFrame来创建窗口程序
第二个填空位置
______( "示范异常" );
需要填入:设置窗口标题的方法
正确答案:super
解释:
- 这里需要调用父类(JFrame)的构造方法设置窗口标题
- 或者也可以填写
setTitle
- 但从上下文看,更可能是构造时初始化标题
第三个填空位置
___________ {
需要填入:异常处理块开始
正确答案:try
解释:
- 后续有对应的catch块
- 需要进行异常捕获的代码必须放在try块中
- 这里包含可能抛出异常的整数转换和除法运算
第四个填空位置
output.setText(_______________________________);
需要填入:格式化后的计算结果输出
正确答案:precision3.format(result)
解释:
- 前面已定义
DecimalFormat precision3 = new DecimalFormat( "0.000" )
- 需要将double类型的result格式化为保留3位小数
- 使用DecimalFormat的format方法
第五个填空位置
_______________________________( this,
需要填入:显示错误对话框的方法
正确答案:JOptionPane.showMessageDialog
解释:
- 查看程序末尾的注释,列出了JOptionPane的常用方法
- 需要显示错误消息,使用showMessageDialog
- 参数顺序为(父组件, 消息内容, 标题, 消息类型)
三、完整正确代码
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Java_3 extends JFrame implements ActionListener {
private JTextField input1, input2, output;
private int number1, number2;
private double result;
public Java_3()
{
super( "示范异常" );
Container c = getContentPane();
c.setLayout( new GridLayout( 3, 2 ) );
c.add( new JLabel( "输入分子", SwingConstants.RIGHT ) );
input1 = new JTextField( 10 );
c.add( input1 );
c.add( new JLabel( "输入分母和回车", SwingConstants.RIGHT ) );
input2 = new JTextField( 10 );
c.add( input2 );
input2.addActionListener( this );
c.add( new JLabel( "计算结果", SwingConstants.RIGHT ) );
output = new JTextField();
c.add( output );
setSize( 425, 100 );
show();
}
public void actionPerformed( ActionEvent e )
{
DecimalFormat precision3 = new DecimalFormat( "0.000" );
output.setText( "" );
try {
number1 = Integer.parseInt( input1.getText() );
number2 = Integer.parseInt( input2.getText() );
result = quotient( number1, number2 );
output.setText(precision3.format(result));
}
catch ( NumberFormatException nfe ) {
JOptionPane.showMessageDialog( this,
"你必须输入两个整数",
"非法数字格式",
JOptionPane.ERROR_MESSAGE );
}
catch ( Exception dbze ) {
JOptionPane.showMessageDialog( 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[] )
{
Java_3 app = new Java_3();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
e.getWindow().dispose();
System.exit( 0 );
}
}
);
}
}
四、关键知识点解析
1. Swing GUI编程
- JFrame:顶级窗口容器
- JTextField:文本输入框
- JLabel:文本标签
- GridLayout:网格布局管理器
- JOptionPane:标准对话框
2. 异常处理机制
- NumberFormatException:数字格式异常
- 自定义异常:通过throw new Exception()抛出
- try-catch-finally:异常捕获处理结构
- throws声明:方法可能抛出的异常
3. 事件处理模型
- ActionListener接口:处理动作事件
- addActionListener:注册事件监听器
- actionPerformed:事件处理方法
五、常见错误分析
GUI组件初始化顺序错误:
- 先添加组件再设置布局
- 组件未添加到内容面板
异常捕获顺序不当:
- 更具体的异常应放在前面
- Exception应放在最后
对话框参数顺序错误:
- showMessageDialog的参数顺序容易混淆
未处理窗口关闭事件:
- 需要添加WindowListener
- 否则点击关闭按钮不会退出程序
六、扩展思考
1. 改进用户体验
- 添加输入验证
- 支持键盘快捷键
- 增加计算历史记录
2. 异常处理最佳实践
- 自定义异常类(如DivideByZeroException)
- 异常日志记录
- 友好的错误提示
3. 多语言支持
使用资源束实现国际化:
ResourceBundle bundle = ResourceBundle.getBundle("Messages");
String title = bundle.getString("window.title");
七、考试技巧
- 先整体后局部:先理解程序整体结构再填空
- 注意上下文:填空处通常与上下文有直接关联
- API文档记忆:记住常用Swing组件和异常类
- 边界测试:考虑各种输入情况(正常、异常、边界值)
八、模拟练习
题目:补全温度转换程序(摄氏转华氏)
public class TempConverter extends JFrame implements ActionListener {
private JTextField celsius, fahrenheit;
public TempConverter() {
super("温度转换");
//*********Found********
Container c = ___________();
c.setLayout(new GridLayout(2, 2));
c.add(new JLabel("摄氏温度", SwingConstants.RIGHT));
celsius = new JTextField(10);
c.add(celsius);
celsius.addActionListener(this);
c.add(new JLabel("华氏温度", SwingConstants.RIGHT));
fahrenheit = new JTextField(10);
c.add(fahrenheit);
setSize(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
//*********Found********
double c = Double._________(celsius.getText());
double f = c * 9 / 5 + 32;
fahrenheit.setText(String.format("%.1f", f));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入数字",
"错误", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new TempConverter();
}
}
答案:
getContentPane
parseDouble
九、总结
通过这道综合应用题,我们掌握了:
- Swing GUI程序的基本结构
- 异常处理机制的实现方式
- 事件驱动编程模型
- Java二级考试中综合题的解题思路
关键点记忆:
- JFrame是顶级窗口容器
- 异常处理try-catch块结构
- JOptionPane显示对话框
- 布局管理器使用GridLayout
希望这篇解析能帮助你在Java二级考试中顺利解决GUI和异常处理相关的题目!