Skip to main content

Need help with error

2 replies [Last post]
TommyEman
Offline
Joined: 2006-02-14

I am working on the code for a small number guess game , the code is as follows:

import java.util.*;

public class guessgame
{
public static void main(String[] args)
{
// Declare variables, setup keyboard input and the
// random number generator
int game_number, user_number;
String continue_pref
Scanner data_input = new Scanner(System.in);
Random generate = new Random();

do
{
// Generate game number
game_number = generate.nextInt(999) + 1;
// The following line is a debug line, comment out
// for real game.
// System.out.printf("Game number:%d%n", game_number);

// Get users first guess
System.out.print("The computer has generated a number.");
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
usernumber = data_input.nextint();

} while ((user_number >= 0) && (user_number <= 1000));

// While user has not guessed right and does not want to quit
while ((user_number == game_number) || (user_number != 0))
{
if (user_number > game_Number)
System.out.printf("You need to guess higher%n");
else
System.out.printf("You need to guess lower%n");

// Get users next guess
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
user_number = data_input.nextInt();

} while ((user_number >= 0) && (user_number <= 1000));
}

if (user_number == 0)
{
// User has guessed right
System.out.printf("%nYou guessed correctly, well done.%nDo you want to play again (y/Y)=Yes: ");
continue_pref = new String(data_input.next());
}
else
{
// User wants to quit
continue_pref = new String("No");
}
} while (continue_pref.equalsIgnoreCase("N"));
}
}

I keep on getting the following error when I compile it. I have no idea what this error means or how to fix it , can anybody help me ? cheers.

C:\guessgame.java:11: ';' expected
Scanner data_input = new Scanner(System.in);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
TommyEman
Offline
Joined: 2006-02-14

Fixed that error now!
I'm new to java so it's all very confusing to me at the moment! Only been learning a few days.

I have now recieved these errors , I realise it's probably cause I haven't delcared the variables for the "usernumber" and "game_number" but not sure how to do this , can anyone help?

m:\guessgame.java:27: cannot find symbol
symbol : variable usernumber
location: class guessgame
usernumber = data_input.nextint();
^
m:\guessgame.java:27: cannot find symbol
symbol : method nextint()
location: class java.util.Scanner
usernumber = data_input.nextint();
^
m:\guessgame.java:34: cannot find symbol
symbol : variable game_Number
location: class guessgame
if (user_number > game_Number)

Could anybody clarify how to fix these ?
Thanks

destin
Offline
Joined: 2006-05-05

Java is case-sensitive. You must refer to a variable [i]exactly[/i] how you declared it. You created variables called [b]game_number[/b] and [b]user_number[/b]. You cannot reference them by [b]game_Number[/b] and [b]usernumber[/b]. Same problem with nextint(). The method is [b]nextInt()[/b], not [b]nextint()[/b].