電卓は、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);}}