Java基础编程题目——捕获异常,除数不能为零

输入两个整数,输出相除的结果,如果除数为 0 则输出“除数不能为0”

import java.util.Scanner;

public class Task {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        try {
            System.out.println(num1 / num2);
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        }
    }
}
  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以这样写一个简单的计算器: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField textField; private JButton[] buttons; private String[] buttonLabels = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", "/", ".", "=" }; private JPanel buttonPanel; public Calculator() { super("Calculator"); // 设置窗口大小和位置 setSize(300, 400); setLocationRelativeTo(null); // 创建文本框和按钮 textField = new JTextField(); buttons = new JButton[buttonLabels.length]; for (int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(buttonLabels[i]); buttons[i].addActionListener(this); } // 创建按钮面板并添加按钮 buttonPanel = new JPanel(new GridLayout(4, 4)); for (JButton button : buttons) { buttonPanel.add(button); } // 将文本框和按钮面板添加到窗口 add(textField, BorderLayout.NORTH); add(buttonPanel); // 显示窗口 setVisible(true); } public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); if (action.equals("=")) { try { // 计算表达式 String expression = textField.getText(); double result = evaluate(expression); textField.setText(Double.toString(result)); } catch (ArithmeticException ex) { // 处理除数的情况 textField.setText("除数不能"); } catch (Exception ex) { // 处理其他异常 textField.setText("Error"); } } else { // 添加数字或运算符 textField.setText(textField.getText() + action); } } private double evaluate(String expression) { // 将表达式转换为逆波兰表达式 String[] tokens = expression.split(" "); Stack<Double> stack = new Stack<>(); for (String token : tokens) { if (token.equals("+")) { double a = stack.pop(); double b = stack.pop(); stack.push(a + b); } else if (token.equals("-")) { double a = stack.pop(); double b = stack.pop(); stack.push(b - a); } else if (token.equals("*")) { double a = stack.pop(); double b = stack.pop(); stack.push(a * b); } else if (token.equals("/")) { double a = stack.pop(); double b = stack.pop(); if (a == 0) { throw new ArithmeticException(); } stack.push(b / a); } else { double number = Double.parseDouble(token); stack.push(number); } } // 计算逆波兰表达式的值 double result = stack.pop(); if (!stack.isEmpty()) { throw new RuntimeException(); } return result; } public static void main(String[] args) { new Calculator(); } } ``` 当输入 `3/0` 时,会抛出 `ArithmeticException` 异常,我们在 `actionPerformed` 方法捕获这个异常并显示提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伶回合

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值