0

I'm making the following method about making an order on a menu with the following code :

   public static double menu_received() {

        Scanner input = new Scanner(System.in);
        int userResponse, total = 0;

        do {

            System.out.println();
            System.out.println("Here is our menu:");
            System.out.println("1. Bulgogi -- $15.00");
            System.out.println("2. Kalbi -- $18.00");
            System.out.println("3. Kimchi Fried Rice -- $16.00");
            System.out.println("Please choose one option at one time using the number (0 to end the order):");

            userResponse = input.nextInt();
            System.out.println("User Menu choice is " + userResponse);

            if (userResponse == 1) {
                System.out.println("Thank you for ordering Bulgogi");
                total = total + 15;
                System.out.println("Your total is $" + total);
            }

            if (userResponse == 2) {
                System.out.println("Thank you for ordering Kalbi");
                total = total + 18;
                System.out.println("Your total is $" + total);
            }

            if (userResponse == 3) {
                System.out.println("Thank you for ordering Kimichi Fried Rice");
                total = total + 16;
                System.out.println("Your total is $" + total);
            }

        } while (userResponse != 0);

        double total2 = total + 2.55;
        // total = (double) total + 2.55;
        System.out.println("Your total after tax is $" + total2);

        return total2;
    }

However the issue I have is when I call it, it does not respond as I want. It doesn't stop to call the menu even after I enter 0. Is there an issue with my condition in the while(userResponse != 0) Can someone explain to me why and how to correct it? Thank you

Here my output :

>
Here is our menu:
1. Bulgogi -- $15.00
2. Kalbi -- $18.00
3. Kimchi Fried Rice -- $16.00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is $15

Here is our menu:
1. Bulgogi -- $15.00
2. Kalbi -- $18.00
3. Kimchi Fried Rice -- $16.00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is $30

Here is our menu:
1. Bulgogi -- $15.00
2. Kalbi -- $18.00
3. Kimchi Fried Rice -- $16.00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is $32.55

Here is our menu:
1. Bulgogi -- $15.00
2. Kalbi -- $18.00
3. Kimchi Fried Rice -- $16.00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is $2.55

PS: Don't mind the statement "Your total after tax is XXXX" - It comes from another method.

Savior
  • 3,225
  • 4
  • 24
  • 48
  • From the output, it looks like you're calling that method twice. – Dawood ibn Kareem Oct 28 '21 at 20:28
  • Oh so it has to do with this line I have in my main I guess : `menu_received(); System.out.println("Value of menu_received : " + menu_received()); tip_calculator(menu_received());` –  Oct 28 '21 at 20:31
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Sam Oct 28 '21 at 20:31
  • @Sam, no, that's entirely unrelated and is NOT the answer to this question. – Dawood ibn Kareem Oct 28 '21 at 20:33
  • @Sam its doesn't.. –  Oct 28 '21 at 20:34
  • @siid_14 Yes. Every time your code has `menu_received()`, the method starts over. Perhaps you want to call it just once, and assign the result to a variable. – Dawood ibn Kareem Oct 28 '21 at 20:35
  • @DawoodibnKareem ah okay I see.. Ima look into that. I thought that `tip_calculator(menu_received());` will have the value of menu_received since I have `return total2` inside the menu_received method. –  Oct 28 '21 at 20:40
  • 1
    I corrected it with the following line : `double value = menu_received(); tip_calculator(value);` And it works, thank you! –  Oct 28 '21 at 20:45
  • Vote retracted. – Sam Oct 29 '21 at 07:24

1 Answers1

1

If I understand your comment above, change your main from :

menu_received(); // First call
System.out.println("Value of menu_received : " + menu_received()); // Second call
tip_calculator(menu_received()); // Third call

To :

double result = menu_received(); // Only one call
System.out.println("Value of menu_received : " + result);
tip_calculator(result);
Florian S.
  • 280
  • 2
  • 5