문자열 연산자를 더하기 (+), 빼기 (-), 곱하기 (*), 나누기 (/) 또는 모듈 (%)으로 입력하면 유효한 입력을 입력해도 while 루프를 계속 입력합니다. while 루프가 num2 변수의 int 값을 입력해야하는 곳에서 잘 작동하기 때문에 문제가 무엇인지 알 수 없습니다.조건이 거짓 일 때 내 프로그램이 while 루프를 입력하는 이유는 무엇입니까?
import java.util.Scanner;
public class PolishNotationCalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
String operator;
System.out.println("Polish notation calculator");
System.out.print("Please enter an operation(+, -, *, /, %) ");
operator = input.nextLine();
while (!operator.equals("+") || !operator.equals("-") || !operator.equals("*") || !operator.equals("/") || !operator.equals("%")) {
System.out.println("Please enter a valid operation ");
operator = input.nextLine();
if (operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/") || operator.equals("%"))
break;
}
System.out.print("");
System.out.print("Please enter the first number ");
num1 = input.nextInt();
System.out.print("Please enter the second number ");
num2 = input.nextInt();
while (num2 == 0 && operator.equals("/")) {
System.out.println("Please pick a non zero number: ");
num2 = input.nextInt();
}
while (num2 == 0 && operator.equals("%")) {
System.out.println("Please pick a non zero number: ");
num2 = input.nextInt();
}
if (operator.equals("+"))
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
else if (operator.equals("-"))
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
else if (operator.equals("*"))
System.out.println(num1 + " * " + +num2 + " = " + (num1 * num2));
else if (operator.equals("/"))
System.out.println(num1 + "/" + num2 + " = " + (num1/num2));
else if (operator.equals("%"))
System.out.println(num1 + " % " + num2 + " = " + (num1 % num2));
}
}
'연산자'는 항상 둘 중 하나 이상이 아니기 때문에 예 : '+'와 같으면'-'와 같지 않습니다. '||'이 아니라'&&'를 의미합니다. –