1

I'm trying to code simple calculator (all in one) using Switch cases in java. I came up with following code so far. However I'm stuck with while loop. I want to keep showing main menu after each case execution until user decides to exit the program.

public static void main(String[] args) 
{   
    Scanner s=new Scanner(System.in);
    System.out.println("Main Menu:");
    System.out.println("1. Addition");
    System.out.println("2. Substraction");
    System.out.println("3. Multipication");
    System.out.println("4. Division");
    System.out.println("Enter your choice: ");
    int i=s.nextInt();

    System.out.println("ENTER FIRST NUMBER ");
    int a=s.nextInt();

    System.out.println("ENTER SECOND NUMBER ");
    int b=s.nextInt();

    int result=0;

    switch(i)
    {
        case 1:
            result=a+b;
            break;
        case 2:
            result=a-b;
            break;
        case 3:
            result=a*b;
            break;
        case 4:
            result=a/b;
            break;

        default:
            System.out.println("Wrong Choice.");

    }

    System.out.println("Answer is "+result);
    }
}

Above code works fine. Program ends itself after execution of user selected choice. I want to put main menu on a repeat.

1
  • 1
    May we know what are the difficulties you are here? At least you can try to incorporate the while loop into your code and we will help to review it. Commented May 21, 2017 at 13:48

3 Answers 3

3

Add a while loop like this:

public static void main(String[] args) {

        // Moved this outside the while loop as davidxxx pointed out +1
        Scanner s = new Scanner(System.in);


        while (true) {
            System.out.println("Main Menu:");
            System.out.println("1. Addition");
            System.out.println("2. Substraction");
            System.out.println("3. Multipication");
            System.out.println("4. Division");
            System.out.println("Enter your choice: ");
            int i = s.nextInt();

            System.out.println("ENTER FIRST NUMBER ");
            int a = s.nextInt();

            System.out.println("ENTER SECOND NUMBER ");
            int b = s.nextInt();

            int result = 0;//'result' will store the result of operation

            switch (i) {
                case 1:
                    result = a + b;
                    break;
                case 2:
                    result = a - b;
                    break;
                case 3:
                    result = a * b;
                    break;
                case 4:
                    result = a / b;
                    break;

                default:
                    System.out.println("Wrong Choice.");

            }

            System.out.println("Answer is " + result);

            System.out.println("Go again?");
            String goAgain = s.next();
            if (!goAgain.equals("y")) {
               break;
            } 

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

6 Comments

Thank you Steven. I wanted to show the main menu without user confirmation. So I modified it a little bit. Didn't knew about using while method like this earlier. Thank you again.
Good answer +1 but creating a Scanner instance at each iteration is not required. It should be moved before the while statement.
No problem! As long as you can manipulate the boolean value you can keep the program running or exit whenever/wherever you want.
@steven you can just break the loop and remove the start variable.
@holi-java Good edit! I'm just used to creating booleans whenever I need a while or do-while loop :)
|
0

Try this:

import java.util.Scanner;

public class Calculator {

    private static final String EXIT = "EXIT";

    public static void main(String[] args) {

        Calculator calc = new Calculator();
        Scanner s = new Scanner(System.in);
        while (true) {
            String res = calc.runCalc(s);
            if (res.equals(EXIT)) {
                break;
            } else {
                System.out.println(res);
            }
        }
    }

    private String runCalc(Scanner s) {
        System.out.println("Main Menu:");
        System.out.println("1. Addition");
        System.out.println("2. Substraction");
        System.out.println("3. Multipication");
        System.out.println("4. Division");
        System.out.println("5. Exit");
        System.out.println("Enter your choice: ");
        int i = s.nextInt();

        if (i == 5) {
            return EXIT;
        }

        System.out.println("ENTER FIRST NUMBER ");
        int a = s.nextInt();

        System.out.println("ENTER SECOND NUMBER ");
        int b = s.nextInt();

        int result = 0;// 'result' will store the result of operation

        switch (i) {
        case 1:
            result = a + b;
            break;
        case 2:
            result = a - b;
            break;
        case 3:
            result = a * b;
            break;
        case 4:
            result = a / b;
            break;

        default:
            return "Wrong Choice.";

        }

        return "Answer is " + result;
    }
}

Comments

0

There is more than one way to achieve this, you can use

  1. while loop.
  2. do-while loop.
  3. for loop.

I think do-while loop is better for your situation. Because either user wants to continue or not you have to proceed one time(before loop false). And you do not want to use another variable for quit the loop.

public static void main(String[] args) 
{   
    Scanner s=new Scanner(System.in);

    int result=0;

    do{
        System.out.println("Main Menu:");
        System.out.println("-1. complete and calculate");
        System.out.println("1. Addition");
        System.out.println("2. Substraction");
        System.out.println("3. Multipication");
        System.out.println("4. Division");
        System.out.println("Enter your choice: ");

        int i=s.nextInt();
        if(i ==-1){
            System.out.println("Answer is "+result);
            return;
        }
        System.out.println("ENTER FIRST NUMBER ");
        int a=s.nextInt();

        System.out.println("ENTER SECOND NUMBER ");
        int b=s.nextInt();


        switch(i)
        {
            case 1:
                result=a+b;
                break;
            case 2:
                result=a-b;
                break;
            case 3:
                result=a*b;
                break;
            case 4:
                result=a/b;
                break;
            default:
                System.out.println("Wrong Choice.");
                break;
        }
    }while(true);

}

5 Comments

Thank you too Blasanka. Our professor didn't cover Boolean quiet yet. so this is what I wanted to do in first place. Boolean seems pretty great too. I'm going to look into Boolean soon.
@GaryM read this post you can find so many tutorials for boolean data types. Dont wait until sir teach you. homeandlearn.co.uk/java/boolean_values.html
@GaryM I think this is the simpler and easy way to achieve your task.
Problem is that this code only calculates the result whenever a user exits the program. Any other calculations done before this are not shown. Also, using a seperate variable that can be used to check if user wants to quit the program is good practice in my opinion. I always learned that producing good variable names for the job they need to do is better for readability/understanding the code. In a few months time you might not know what variable i was used for, or how many times and for what different tasks... Anyway, I think OP has been helped quit good :)
He didn't ask for pointed that he want to see answer in each and every iteration. I just followed his code. Also thats not my variable naming convention. But i can be change to choice quit is also a choice. But I agree with you that differentiate both calc operation and quite using different variables. I tried to make this simple program and this question about how to loop. I just tried to keep this simple and easiest to biginner.

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.