Results 1 to 2 of 2

Thread: Guessing Game using do...while loop (Help)

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Guessing Game using do...while loop (Help)

    I need to take this program that I did and do it the same way, but using a do...while loop rather than a while loop. Here is the general outline below the program. Thanks


    ****************************************************************
    // Guess.java
    // Tanesha McClain
    // Play a game where the user guesses a number from 1 to 10
    //
    // ****************************************************************
    import java.util.Scanner;
    import java.util.Random;

    public class Guess
    {
    public static void main(String[] args)
    {
    int numToGuess; //Number the user tries to guess
    int guess; //The user's guess
    int count = 1;
    int guesslow =0, guesshigh =0; // number of guesses
    Scanner scan = new Scanner(System.in);
    Random generator = new Random();

    //randomly generate the number to guess
    numToGuess = generator.nextInt(10) + 1;
    //print message asking user to enter a guess
    System.out.println("Please enter a guess: ");
    //read in guess
    guess = scan.nextInt();

    while (guess != numToGuess) //keep going as long as the guess is wrong
    {
    //print message saying guess is wrong
    System.out.println("Sorry try again!");
    if (guess < numToGuess)
    {
    System.out.println("Your guess is too low");
    guesslow++;

    }
    else
    {
    System.out.println("Your guess is too high");
    guesshigh++;
    }
    //get another guess from the user
    System.out.println("Please enter a guess: ");
    guess = scan.nextInt();
    count++;

    }



    //print message saying guess is right
    System.out.println("Congratulations, you win! " + "This is the number of guesses you took is: " + count + " The total number of highes " + guesshigh + " and lows are " + guesslow + ".");
    }
    }


    The Do...while loop program

    //set up (initializations of the counting variables)

    do
    {
    //read in a guess

    //checkt he guess and print appropriate message

    }
    while ( Condition );

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Guessing Game using do...while loop (Help)

    just move the while (guess != numToGuess); to the end of the loop and then put a do in its place.
    Code:
    // Guess.java
    // Tanesha McClain
    // Play a game where the user guesses a number from 1 to 10
    //
    // ****************************************************************
    import java.util.Scanner;
    import java.util.Random;
    
    public class Guess
    {
      public static void main(String[] args)
      {
        int numToGuess; //Number the user tries to guess
        int guess; //The user's guess
        int count = 1;
        int guesslow =0, guesshigh =0; // number of guesses
        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        
    //randomly generate the number to guess
        numToGuess = generator.nextInt(10) + 1;
    //print message asking user to enter a guess
        System.out.println("Please enter a guess: ");
    //read in guess
        guess = scan.nextInt();
        
        do //keep going as long as the guess is wrong
        {
    //print message saying guess is wrong
          System.out.println("Sorry try again!");
          if (guess < numToGuess)
          {
            System.out.println("Your guess is too low");
            guesslow++;
            
          }
          else
          {
            System.out.println("Your guess is too high");
            guesshigh++;
          }
    //get another guess from the user
          System.out.println("Please enter a guess: ");
          guess = scan.nextInt();
          count++;
        }
        while (guess != numToGuess);
        //print message saying guess is right
        System.out.println("Congratulations, you win! " + "This is the number of guesses you took is: " + count + " The total number of highes " + guesshigh + " and lows are " + guesslow + ".");
      }
      
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width