Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Do - While Loop

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Do - While Loop

    Hello, I'm making the following method about making an order 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 :

    Welcome to our super beautiful Restaurant that you will never ever find anywhere else than here at this right moment!


    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

  2. #2
    Junior Member
    Join Date
    Jul 2019
    Posts
    26
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Do - While Loop

    this code works as I expect and ends after 0.
    What you have in main() ?

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM

Tags for this Thread