# Do while loop problem

• October 26th, 2011, 02:08 PM
kratos75
Do while loop problem
Hi all,
My program is pretty simple, it involves a guessing game where the user is prompted to enter an amount of money to start with. After that the user guesses a number from 1 to 100 and depending on the number guessed, the game lets you know if it's too high or low. With that said, I am trying to insert a loop to prompt an error if the user enters an amount that's over \$300 and allow the user to re-enter an amount. If the amount initially or after the error is <= to 300, then the user may proceed with the game. This is the code before the do/while loop is implemented:

Code :

```import java.io.IOException; import java.util.Random; import java.util.Scanner;   public class guessinggame2 { public static void main(String[] args) throws IOException { Scanner reader = new Scanner(System.in); Random random = new Random();   int randomNumber, dollars = 0, balance = dollars, guessedNumber = 0; char runAgain = 'y';   randomNumber = random.nextInt(101); System.out.print("Welcome to the Guessing Game!" + "\n" ); System.out.print("The rules are simple, you have to guess the correct number and " + "\n"); System.out.print("if it is wrong, you lose \$10 and will be prompted on how close " + "\n"); System.out.print("to the number you are. If you guess the number right, you win " + "\n"); System.out.print("\$100 and can choose whether or not to continue playing or not." + "\n"); System.out.print("" + "\n"); System.out.print("How many dollars do you wish to start off with? (Maximum of \$300) " + "\n"); dollars =reader.nextInt(); while (runAgain == 'y' || runAgain == 'Y') { System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); while (guessedNumber != randomNumber) { if (guessedNumber < randomNumber) { dollars -= 10; System.out.println("Too low!\nGuess again!\nYour balance is: \$" + dollars + "\n"); }   if (guessedNumber > randomNumber) { dollars -= 10; System.out.println("Too high\nGuess again!\nYour balance is: \$" + dollars + "\n"); }   System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); } dollars += 50; System.out.println("Congratulations, you got it!" +"\n"); System.out.println("Your balance is: \$" + dollars + "\n"); System.out.print("Play again (y/n)? "); runAgain = (char)System.in.read();   if (runAgain == 'n' || runAgain == 'N')   { System.exit(0); } if (runAgain == 'y' || runAgain == 'Y') randomNumber = random.nextInt(101); } } }```

And this is the code with the attempted do/while loop, which doesn't work:

Code :

```import java.io.IOException; import java.util.Random; import java.util.Scanner;   public class guessinggame2 { public static void main(String[] args) throws IOException { Scanner reader = new Scanner(System.in); Random random = new Random();   int randomNumber, dollars = 0,   guessedNumber = 0; char runAgain = 'y';   randomNumber = random.nextInt(101); System.out.print("Welcome to the Guessing Game!" + "\n" ); System.out.print("The rules are simple, you have to guess the correct number and " + "\n"); System.out.print("if it is wrong, you lose \$10 and will be prompted on how close " + "\n"); System.out.print("to the number you are. If you guess the number right, you win " + "\n"); System.out.print("\$100 and can choose whether or not to continue playing or not." + "\n"); System.out.print("" + "\n"); System.out.print("How many dollars do you wish to start off with? (Maximum of \$300) " + "\n"); dollars =reader.nextInt();   do{   System.out.println("Error, please enter a valid amount: "); dollars =reader.nextInt(); while (dollars >= 301);     while (runAgain == 'y' || runAgain == 'Y') { System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); while (guessedNumber != randomNumber) { if (guessedNumber < randomNumber) { dollars -= 10; System.out.println("Too low!\nGuess again!\nYour balance is: \$" + dollars + "\n"); }   if (guessedNumber > randomNumber) { dollars -= 10; System.out.println("Too high\nGuess again!\nYour balance is: \$" + dollars + "\n"); }   System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); } dollars += 50; System.out.println("Congratulations, you got it!" +"\n"); System.out.println("Your balance is: \$" + dollars + "\n"); System.out.print("Play again (y/n)? "); runAgain = (char)System.in.read();   if (runAgain == 'n' || runAgain == 'N')   { System.exit(0); } if (runAgain == 'y' || runAgain == 'Y') randomNumber = random.nextInt(101); } } } }```

Any help/suggestions are greatly appreciated.
Thank you.
• October 27th, 2011, 06:04 AM
daniel.j2ee
Re: Do while loop problem
``` do { System.out.println("Error, please enter a valid amount: "); dollars =reader.nextInt(); while (dollars >= 301);```