Hi. Im doing a few exercises because I have a test tomorrow (Wish me luck!). This particular exercise asks for a menu calculator. The Menu is like:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Exit

Im using do..while and the switch case is in the do..while. and im having trouble with the choice number 5. Since im using do while the menu will appear again after the result is calculated until the user picks choice 5 but if i choose 5, program does not exit but insted continues on. This is my code:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //initialize scanner 
        Scanner sc = new Scanner(System.in);

        //initialize to get user input
        int choice, firstNum, secondNum, result = 0;

        //reiterate menu after result if choice 5 not chosen
        do{
            //print out menu
            System.out.println("Operations:\n");
            System.out.println("1. Addition");
            System.out.println("2. Subtraction");
            System.out.println("3. Multiplication");
            System.out.println("4. Division");
            System.out.println("5. Exit\n");

            System.out.println("Enter your choice : ");
            choice = sc.nextInt();

            System.out.println("Enter first operand : ");
            firstNum = sc.nextInt();
            System.out.println("Enter second opernad : ");
            secondNum = sc.nextInt();

            switch(choice){
                case 1 : result = firstNum + secondNum;
                        break;
                case 2 :result = firstNum - secondNum;
                        break;
                case 3 :result = firstNum * secondNum;
                        break;
                case 4 :result = firstNum / secondNum;
                        break;
                case 5 : System.exit(0);

                default : System.out.println("Please choose between 1 - 5 only.");
            }

            System.out.println("Result : " + result);


        }while(choice!= 5);

    }

}

Thanks in advance.

try with changing this:

case 5 : System.exit(0);
                default : System.out.println("Please choose between 1 - 5 only.");

to

case 5 : break;
                default : System.out.println("Please choose between 1 - 5 only.");

and put the System.exit(0); statement after your while loop.

I don't really see why your code doesn't work (immediately), maybe you forgot to resave/recompile?

I saved and compiled. and when i run the program

  1. Choose operation
  2. Enter first number
  3. Enter second number
  4. Result
    and
    Repeat

choice 5 is suppose to stop that process but when choice 5 is entered it still goes to step 2 ...

I amended as stultuske suggested but still goes to step 2 either way.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //initialize scanner 
        Scanner sc = new Scanner(System.in);

        //initialize to get user input
        int choice;
        float firstNum, secondNum, result = 0;

        //reiterate menu after result if choice 5 not chosen
        do{
            //print out menu
            System.out.println("Operations:\n");
            System.out.println("1. Addition");
            System.out.println("2. Subtraction");
            System.out.println("3. Multiplication");
            System.out.println("4. Division");
            System.out.println("5. Exit\n");

            System.out.println("Enter your choice : ");
            choice = sc.nextInt();

            System.out.println("Enter first operand : ");
            firstNum = sc.nextFloat();
            System.out.println("Enter second opernad : ");
            secondNum = sc.nextFloat();

            switch(choice){
                case 1 : result = firstNum + secondNum;
                        break;
                case 2 :result = firstNum - secondNum;
                        break;
                case 3 :result = firstNum * secondNum;
                        break;
                case 4 :result = firstNum / secondNum;
                        break;
                case 5 : break;
                default : System.out.println("Please choose between 1 - 5 only.");
            }

            System.out.println("Result : " + result);


        }while(choice!= 5);

        System.exit(0);

        sc.close();
    }

}

True, but if you select case 5, the program runs once more and then after that it exits. I would have used a while loop rather than a do-while. Maybe, but more expert members might be able to tell you more, the condition 'choice' is evaluated at the beginning, so when you select 5 and reach the end of the loop (while(choice!= 5);) choice isn't evaluated again, that's why I'm saying, maybe you want to use a while loop. But wait for somebody else to confirm this, as I may be wrong

if you enter 5 as the first choice, of course it will go through the entire code and get it all, since the condition is only tested after you input the information.
If you don't want that, use a while statement instead of a do-while, and have the first time the choice read before the loop starts, and enter the next choice as last statement of the loop.

oh okay.

um, like this?

enter choice
while(choice != 0){
    switch(choice)
    enter choice
}

Look at your code and think it as a block-by-block (from original 1st post).

Line 13 - start the loop
Lines 15~23 - show a menu and wait for user to enter a choice
Lines 25~28 - accept user input REGARDLESS what user choice is
Lines 30~42 - do whatever user has chosen
Line 47 - exit the loop if user choice is '5'

Do you see the issue here? The 3rd block is the problem for you because it is executed without checking what user choice is so far. There are proper ways to fix this, but for you right now, I would do a quick patch job. If you add if (choice>=1 && choice<=4) { to crop around lines 25~28, it will work as intended.

if (choice>=1 && choice<=4) {
  System.out.println("Enter first operand : ");
  firstNum = sc.nextFloat();
  System.out.println("Enter second opernad : ");
  secondNum = sc.nextFloat();
}

oh okay.
um, like this?

 enter choice
     while(choice != 0){
         switch(choice)
         enter choice
     }

That's the short version, but basically, yes.

Oh cool, glad I got my first Java suggestion right :-)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.