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 );
****************************************************************
// 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 );