(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(); } } } }