PDA

Click to See Complete Forum and Search --> : Guessing Game using do...while loop (Help)


Tmcclain
Oct 31st, 2006, 01:26 PM
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 );

TBeck
Oct 31st, 2006, 01:39 PM
just move the while (guess != numToGuess); to the end of the loop and then put a do in its place.

// 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 + ".");
}

}