1

I'm doing a simple Java calculator for school which works just fine. However, I need to add a while loop that asks the user if they want to continue yes/no. I can't figure out where I'm supposed to put the while statement though. I've tried putting above the if statement, I've tried below it and adding it into each if and else if statement and still can't get it to work. Where are you supposed to put a while loop within a if and else if statement to get all choices to run the while loop?

while (input.toUpperCase().equals("y"))
System.out.println("Do you want to continue? (y/n)");
input = calc.nextLine();

import java.util.Scanner;

public class loops { 

public static void main(String[] args) {

Scanner calc = new Scanner (System.in);
double firstNum, secondNum, answer;
String input = "";

System.out.println("what would you like to do");
System.out.println("a. Add two numbers. ");
System.out.println("b. Subtract two numbers. ");
System.out.println("c. Multiply two numbers. ");
System.out.println("d. Divide two numbers. ");
System.out.println("Select a letter");
input.calc.nextLine();

if (input.equals("a")) {

System.out.println("Please enter your first number: ");
firstNum = calc.nextDouble();
System.out.println("Enter your second number: ");
secondNum = calc.nextDouble();

answer = firstNum + secondNum;
System.out.println("your answer is: " + String.valueOf (answer));

else if (input.equals("b")) {
System.out.println("Please enter your first number: ");
firstNum = calc.nextDouble();
System.out.println("Enter your second number: ");
secondNum = calc.nextDouble();

answer = firstNum - secondNum;
System.out.println("your answer is: " + String.valueOf (answer));
}

}
}
}
5
  • Can you post the whole snippet? It would be helpful to see the else block as well. Commented Mar 28, 2016 at 17:56
  • use do-while statement. put your calculation functionality inside of do block which makes it executes at least one time, and then checks continue or not in your while expression Commented Mar 28, 2016 at 17:57
  • 2
    input.toUpperCase().equals("y") This condition will never equal true. You're converting the user input to upper case then checking y as lower case. Commented Mar 28, 2016 at 18:01
  • Taking a stab here, I'd say move your entire if-statement within your while loop, above System.out.print("Do you want to continue? (y/n)"); And fix your while loop condition as Rick correctly pointed out. Commented Mar 28, 2016 at 18:01
  • I updated it to add the else if statement Commented Mar 28, 2016 at 18:16

3 Answers 3

1

I think in this case you should put the if-else inside the while. It looks like you want to execute the if until a certain case, so the proper way to do that is to surround the if with some kind of conditional loop (a do-while might also be appropriate here, if you wanted it to run through at least once).

Edit: You will also want to fix the condition on the while. As noted in the comments on the question, input.toUpperCase().equals("y") will never evaluate to true.

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

2 Comments

I'm at work right now, but I just used the text editor to add in the else if statement. So do I put the while loop above and just nest the else if in there?
Yes. Do what's in your while now first, then the if-else.
0

As pointed out earlier do while loop should be used, but if you want to go with while then Your Code should look like this ->

Scanner calc = new Scanner(System.in);
double firstNum, secondNum, answer;
String input ="y";



  while (input.toUpperCase().equals("y")) {
        System.out.print("Do you want to continue? (y/n)");
        input = calc.nextLine();
        if (input.equals("a")) {
             System.out.print("Please ,enter your first number:  ");
             firstNum = calc.nextDouble();
             System.out.print("Enter your second number: ");
             secondNum = calc.nextDouble();

             answer = firstNum + secondNum;
             System.out.println("Your answer is: " + String.valueOf(answer));    
             }
    }

1 Comment

Ohhh ok, so I just nest everything in the while loop or the do while loop then, correct?
0

You can use do-while loop to implement your functionality. Do-while will execute at least once and then checking continuous running expression is true or false.

Your code has 2 responsibilities:

1. calculate the sum of 2 numeric inputs.
2. checks continuous calculating or not.

One of the solutions is separating those two features and makes it easier to add/remove any of them. It would be easy to test/debug your code.

import java.util.*;

public class Test{
    static Scanner calc = new Scanner(System.in);
    static String input ="";
    static boolean keepRunning = false;
    double firstNum, secondNum, answer;


    public static void main(String[]args){
        Test test = new Test();
        do{
            test.calculate(calc);  // calculate your 2 numeric inputs

            // update boolean flag indicate continuous calculating or stop
            System.out.print("Do you want to continue? (y/n)");
            input = calc.next();
            keepRunning = input.toLowerCase().equals("y");
        }while(keepRunning);

    }

    public void calculate(Scanner calc){
        System.out.print("Please ,enter your first number:  ");
        firstNum = calc.nextDouble();
        System.out.print("Enter your second number: ");
        secondNum = calc.nextDouble();

        answer = firstNum + secondNum;
        System.out.println("Your answer is: " + String.valueOf(answer));
    }
}

2 Comments

Ok, that all makes sense. I'll give that a try when I get home. Thanks for the help.
@Nooberistic no problem, hope it helps

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.