//Loop that isn't working. I keep pressing a number that is 1,2,3,4, or 5 but it won't exit the loop. The operator seems to be assigned the value that I input but it still will not exit the while loop. I'm trying to write a basic calculator with simple math operations but this turned into a very annoying problem.
import java.util.Scanner;
public class BasicCalculatorTwo {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int operator;
double fnum, snum, answer;
operator = 0;
System.out.println("Enter first number : ");
fnum = scanner.nextDouble();
System.out.println("Enter second number : ");
snum = scanner.nextDouble();
while(operator != 1 || operator != 2 || operator != 3 || operator != 4 || operator != 5 ){
System.out.println();
System.out.println(fnum + " ? " + snum + " = ");
System.out.println("1 : Add");
System.out.println("2 : Subtract");
System.out.println("3 : Multiply");
System.out.println("4 : Divide");
System.out.println("5 : Modularize");
operator = scanner.nextInt();
}
switch(operator){
case 1:
answer = fnum + snum;
break;
case 2:
answer = fnum - snum;
break;
case 3:
answer = fnum * snum;
break;
case 4:
answer = fnum / snum;
break;
case 5:
answer = fnum % snum;
break;
default:
break;
System.out.println(fnum + " ? " + snum + " = " + answer);
scanner.close();
}
}
}
&¬||.operator != 1 OR operator != 2 OR operator != 3 OR operator != 4 OR operator != 5- this will always betrue. Maybe you meant to use==or&&? Anytime you have an infinite loop, look at your control expression and your control variable.