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 7 of 7

Thread: Trouble with a "do while loop."

Threaded View

  1. #1
    Junior Member
    Join Date
    Nov 2023
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble with a "do while loop."

    (Apologies if this is in the wrong forum topic - is this post better for a different version of Java? If so, which one?) I'm struggling to learn Java, so I made myself an exercise to play around with in learning conditional statements. When I run this code, and it gets to line 70, it duplicates the part that asks if the user wants to take anymore fruits. I haven't been able to figure out what to do to make it only ask that once. How do I get this thing to stop asking that question twice before waiting for the user to give a "yes" or "no" answer?

    Here's the entire code:

     
    // Import the Scanner class for user input
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            // Create a Scanner object for user input
            Scanner scanner = new Scanner(System.in);
     
            // Get the number of apples, oranges, and bananas
            System.out.println("Enter the number of apples in the basket: ");
            int appleCount = scanner.nextInt();
     
            System.out.println("Enter the number of oranges in the basket: ");
            int orangeCount = scanner.nextInt();
     
            System.out.println("Enter the number of bananas in the basket: ");
            int bananaCount = scanner.nextInt();
     
            // Check if there are enough apples
            if (appleCount > 0) {
                System.out.println("There are " + appleCount + " apples available.");
                System.out.println("How many apples do you want to take? ");
                int applesToTake = scanner.nextInt();
     
                if (applesToTake <= appleCount) {
                    appleCount -= applesToTake;
                    System.out.println("You took " + applesToTake + " apples.");
                } else {
                    System.out.println("Not enough apples. You can only take " + appleCount + " apples.");
                }
            } else {
                System.out.println("There are no apples in the basket.");
            }
     
            // Check if there are enough oranges
            if (orangeCount > 0) {
                System.out.println("There are " + orangeCount + " oranges available.");
                System.out.println("How many oranges do you want to take? ");
                int orangesToTake = scanner.nextInt();
     
                if (orangesToTake <= orangeCount) {
                    orangeCount -= orangesToTake;
                    System.out.println("You took " + orangesToTake + " oranges.");
                } else {
                    System.out.println("Not enough oranges. You can only take " + orangeCount + " oranges.");
                }
            } else {
                System.out.println("There are no oranges in the basket.");
            }
     
            // Check if there are enough bananas
            if (bananaCount > 0) {
                System.out.println("There are " + bananaCount + " bananas available.");
                System.out.println("How many bananas do you want to take? ");
                int bananasToTake = scanner.nextInt();
     
                if (bananasToTake <= bananaCount) {
                    bananaCount -= bananasToTake;
                    System.out.println("You took " + bananasToTake + " bananas.");
                } else {
                    System.out.println("Not enough bananas. You can only take " + bananaCount + " bananas.");
                }
            } else {
                System.out.println("There are no bananas in the basket.");
            }
     
            // Ask if the user wants to take any more fruits
            String choice;
            do {
                System.out.println("Do you want to take any more fruits? (Enter 'yes' or 'no')");
                choice = scanner.nextLine();
            } while (!choice.equalsIgnoreCase("yes") && !choice.equalsIgnoreCase("no"));
     
            while (choice.equalsIgnoreCase("yes")) {
                // Check if there are any apples left
                if (appleCount > 0) {
                    System.out.println("There are " + appleCount + " apples left.");
                    System.out.println("Do you want to take any more apples? (Enter 'yes' or 'no')");
                    choice = scanner.nextLine();
     
                    if (choice.equalsIgnoreCase("yes")) {
                        System.out.println("How many more apples do you want to take? ");
                        int applesToTake = scanner.nextInt();
     
                        if (applesToTake <= appleCount) {
                            appleCount -= applesToTake;
                            System.out.println("You took " + applesToTake + " more apples.");
     
                            if (orangeCount > 0) {
                                System.out.println("There are " + orangeCount + " oranges left.");
                                System.out.println("Do you want to take any more oranges? (Enter 'yes' or 'no')");
                                choice = scanner.nextLine();
     
                                if (choice.equalsIgnoreCase("yes")) {
                                    System.out.println("How many more oranges do you want to take? ");
                                    int orangesToTake = scanner.nextInt();
     
                                    if (orangesToTake <= orangeCount) {
                                        orangeCount -= orangesToTake;
                                        System.out.println("You took " + orangesToTake + " more oranges.");
                                    } else {
                                        System.out.println("There are no more oranges left.");
                                    }
     
                                    if (bananaCount > 0) {
                                        System.out.println("There are " + bananaCount + " bananas left.");
                                        System.out.println("Do you want to take any more bananas? (Enter 'yes' or 'no')");
                                        choice = scanner.nextLine();
     
                                        if (choice.equalsIgnoreCase("yes")) {
                                            System.out.println("How many more bananas do you want to take? ");
                                            int bananasToTake = scanner.nextInt();
     
                                            if (bananasToTake <= bananaCount) {
                                                bananaCount -= bananasToTake;
                                                System.out.println("You took " + bananasToTake + " more bananas.");
                                            } else {
                                                System.out.println("There are no more bananas left.");
                                            }
     
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Thank the user and close the scanner object
                    System.out.println("Thank you and have a nice day!");
                    scanner.close();
                }
            }
        }
    }
    Last edited by ChewyGrizzy; November 21st, 2023 at 10:44 PM. Reason: Not sure where to put this post after all. Newb over here.

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  4. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  5. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM

Tags for this Thread