PDA

View Full Version : Guessing game help


NoobCoder8769
01-03-2010, 07:09 PM
I'm trying to make a guessing game where I have to allow the user select among a few levels, and then:
-tell the user whether the guess was too high,too low or correct.
-keep track of the number of guesses
- and tell the user to guess the number within the range of the random number
(ex. Intermediate says "Enter a number between 1 and 100.)

I'm getting numerous errors and I have a feeling I'm going about this all wrong :/...


import java.util.Scanner;
public class GuessingGame
{

public static void main(String[] args)

{
Scanner gamescanner = new Scanner(System.in);
System.out.println("This is a number guessing game");
System.out.println("What level of difficulty would you like?");
System.out.println("1. Beginner's level");
System.out.println("2. Intermediate level");
System.out.println("3. Advanced level");
System.out.println("4. Expert level");
System.out.println("9. End the game");
System.out.println("Please input the number choice or else the game will not run:");
int gamelevel = gamescanner.nextint();

int rndm;
if (gamelevel = 1)
{
rndm = (Math.random()*10)+1;
System.out.println("Enter a number between 1-10");
}
else if (gamelevel = 2)
{
rndm = (Math.random()*100)+1;
System.out.println("Enter a number between 1-100");
}
else if (gamelevel =3)
{
rndm = (Math.random()*1000)+1;
System.out.println("Enter a number between 1-1000");
}
else if (gamelevel =4)
{
rndm = (Math.random()*10000)+1;
System.out.println("Enter a number between 1-10");
}



while (int guess!=rndm)
{
System.out.println("What is your guess?");
int guess1 = gamescanner.nextint();
guess = guess1;
count = count +1;
if (guess > rndm)
{
System.out.println("Your guess is too high");
}
else if (guess < rndm)
{
System.out.println("Your guess is too low");
}
}


System.out.println("You're right!!!You win!!");
System.out.println("It took you "+count+" guesses.");




}
}

Old Pedant
01-03-2010, 08:18 PM
Your logic looks fine. Could be coded more simply, but nothing obviously wrong.

So the errors are probably minor ones.

Why don't you
(1) show us the errors
(2) tackle them one at a time, at least making a guess as to what they mean.

Old Pedant
01-03-2010, 08:20 PM
I will give you one hint: Watch out for the difference in meaning between = and ==.

Your game will always play at level 1, no matter what the user chooses.

NoobCoder8769
01-03-2010, 08:32 PM
I will give you one hint: Watch out for the difference in meaning between = and ==.

Your game will always play at level 1, no matter what the user chooses.

Thanks, I already had fixed it and yes that was one of the minor errors. But for some reason it said that some of my variables might not have been declared and I ended up having to put them at the top and set them equal to 0...weird :/.

Old Pedant
01-04-2010, 01:06 AM
Think about it a moment: If the gamelevel is not 1 through 4, what value will rndm have? Answer: No value at all. Result: an uninitialized variable.

If you had a final "else" where you assigned a default value to rndm, then there would be no path through the code where it doesn't get a value.