• February 12th, 2013, 08:57 PM
kevinsauerzxc
My codes are below, I'm trying to program a guessing game. The computer will generate a random number for each game, and the user will guess what number it is for infinite number of tries. The game has to be repeated if the user wanted to.

Here is my problem, whenever I press 1 to repeat and guess the number, the errors occur. Can someone please test this and help me fix my error, thank you.

import java.util.Scanner;
import java.util.Random;

public static void main (String args []){

Scanner keyboard = new Scanner (System.in);
Random number = new Random();
double userGuess = 0.0;
boolean right = false;
double totalGuess = 0.0;
double continueGame = 0.0;
double numberOfGames = 0.0;

do
{
int computerNumber = number.nextInt(100) + 1;

numberOfGames++;

double guessesEachGame = 0.0;

do
{
System.out.println("The computer's number is "+computerNumber+"");

System.out.print("Please guess a number between 1 and 100: ");
userGuess = keyboard.nextDouble();

guessesEachGame ++;
totalGuess++;

{

if (userGuess == computerNumber)
{
right = true;
}
else if (userGuess > computerNumber)
{
}
else if (userGuess < computerNumber)
{
}
}
} while (right == false);

System.out.println("The computer's number is "+computerNumber+"");

System.out.println("Your guess is right, the total number of guess is "+guessesEachGame+"");

System.out.println("Do you wish to continue? Type 1 then press enter to continue and press any other number to stop the game.");

continueGame = keyboard.nextDouble();

} while (continueGame == 1);
System.out.println("The total of guess for all games is "+totalGuess+"");

System.out.println("Your overall average number of guesses is "+totalGuess/numberOfGames+"");

} // end main
} // end class
• February 12th, 2013, 09:03 PM
javaiscool