1

//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();
        }
    }
}
3
  • 4
    The while loop condition is always true. The operator variable is always either not 1 or not 2 -- think of this logically and you'll see. You want to use && not ||. Commented Sep 29, 2015 at 2:09
  • operator != 1 OR operator != 2 OR operator != 3 OR operator != 4 OR operator != 5 - this will always be true. Maybe you meant to use == or &&? Anytime you have an infinite loop, look at your control expression and your control variable. Commented Sep 29, 2015 at 2:12
  • Oh, i literally just read the while loop to myself out loud and now I see why it is stuck in an infinite loop. Commented Sep 29, 2015 at 2:17

1 Answer 1

2

You loop conditional is the problem.

while (operator != 1 || operator != 2 || operator != 3 || operator != 4 || operator != 5)

It should be

while (operator != 1 && operator != 2 && operator != 3 && operator != 4 && operator != 5)

Basically, you're saying that if the operator is != 1, then do the loop. Likewise each of the others. If you were to utilize && operators instead of || it would work much better.

Really, what you want to say is that operator is > 1 && < 5, then loop, otherwise break.

while(operator < 1 || operator > 5)
{
  DoPrintStuffHere();
}

Think about it logically, you want any number less than 1 OR greater than 5 to loop again.

Sign up to request clarification or add additional context in comments.

3 Comments

When i add the "while(operator < 1 && operator > 5)" the while loop doesn't run.
Oops, yeah, I did the same thing you did. This is always false. You cannot be always < 1 && > 5. In this case, you would utilize the or operator ||
@Hovercraft full of eels gave the same answer in a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.