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 problem

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

    Default 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:

    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:

    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.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Do while loop problem

    Your problem is here:
      do {
    System.out.println("Error, please enter a valid amount: ");
    dollars =reader.nextInt();
    while (dollars >= 301);
    Is a syntax error.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:02 PM.

Similar Threads

  1. While loop problem
    By ak120691 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:09 PM
  2. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 12:12 PM
  3. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  4. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM