Nooberistic
Nooberistic

Reputation: 11

Adding while loop with if statement java

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));
}

}
}
}

Upvotes: 1

Views: 4216

Answers (3)

Haifeng Zhang
Haifeng Zhang

Reputation: 31905

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));
    }
}

Upvotes: 0

Ajinkya Patil
Ajinkya Patil

Reputation: 741

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));    
             }
    }

Upvotes: 0

senschen
senschen

Reputation: 804

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.

Upvotes: 1

Related Questions