-1

每当除以 0 或使用非整数时,我的计算器就会终止。我无法让我的 while 循环工作。

谁能帮忙,并解释我如何解决这个问题?

编辑:

它仍然终止。

package b;

import java.util.*;


public class Calculator3{


    static boolean _state = true;
    public static void main (String[] args){

        System.out.println("Usage: operand1 operator operand2");
        System.out.println("Operands are integers");
        System.out.println("Operators: + - * /");

        Scanner in = new Scanner(System.in);

        do{


        int result = 0;
        int operand1 = 0;
        int operand2 = 0;
        String operator = " ";
        char op = ' ';

        try{
        operand1 = in.nextInt();
        operator = in.next();
        op = operator.charAt(0);
        operand2 = in.nextInt();}

        catch (InputMismatchException e){
            System.out.println("One or both of the operands are non-integers. Please check your operands");
            break;}





            try{
                switch (op){
                    case '+': result = operand1 + operand2;
                    break;
                    case '-': result = operand1 - operand2;
                    break;
                    case '*': result = operand1 * operand2;
                    break;
                    case '/': result = operand1 / operand2;
                    break;
                    default: System.out.println("Unknown Operator");
                    break;}

            }

            catch(RuntimeException e){
                System.out.println("Operand2 cannot be 0");
                System.exit(0);}


            finally{
                System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result);}
        }
            while (_state = true);}}
4

5 回答 5

4
} while (_state = true);

应该

} while (_state == true);

或更好

} while (_state);

您需要避免以下情况以避免提前退出循环:

  • 以错误的顺序输入值和运算符
  • 使用将导致ArithmeticException诸如的表达式3/0
于 2013-07-01T21:23:42.420 回答
1

如果您尝试除以零,则会抛出异常。

您的 catch 块调用System.exit().

这就是为什么当您除以零时您的代码“终止”的原因。

在将其用于算术之前,您应该验证您的输入(确保它是一个实数)。

我会做这样的事情:

do {
    try {
        char op = operator.charAt(0);
        int operand1 = in.nextInt();
        int operand2 = in.nextInt();
        if (operand2 == 0 && op == '/') {
            System.out.println("Cannot divide by zero");
            continue;    
        }
        .
        .
        .
        switch (op) {
            .
            .
            .
        }
    }
    catch (NumberFormatException nfe) {
        System.out.println("Invalid input");
        continue; 
    }
} while (_state);

这只是许多可能的方法之一。

查看您的 try/catch 逻辑并考虑在抛出不同的异常时会发生什么。

于 2013-07-01T21:31:42.630 回答
1

ArithmeticException是一个RuntimeException. catch 块触发并被System.exit(0)执行。想想你想如何处理错误。退出应该不是吧。

于 2013-07-01T21:34:01.143 回答
0

尝试这个

case '/':
    if (operand2 == 0) {
        System.out.println("Operand2 cannot be 0");
        continue;
    } else {
        result = operand1 / operand2;
        break;
    }
于 2013-07-01T21:42:07.387 回答
0

您的程序在 2 个条件下退出。首先是 catch (InputMismatchException e){ System.out.println("One or both of the operands are non-integers. Please check your operands"); break;}

如果您仍想在非整数(即双精度、浮点数等)的情况下继续,请删除此中断。

其次是

` catch(RuntimeException e){
            System.out.println("Operand2 cannot be 0");
            System.exit(0);}`

System.exit(0) 导致此 while 循环中断,如果要继续,请删除 System.exit(0);

于 2013-07-01T21:47:31.370 回答