Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help with Guessing Game (Head First Java)

 
Siki Rikishi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm sure this is a glaring error but I'm not seeing it. :-) Here's my code, followed by the error messages. Thanks in advance!
public class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9...");
while (true) {
System.out.println("Number to guess is " + targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println("Player one guessed " + guessp1);

guessp2 = p2.number;
System.out.println("Player two guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three guessed" + guessp3);
if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 = targetNumber) {
p2isRight = true;
}
if (guessp3 = targetNumber) {
p3isRight = true;
}
if (p1isRight || p2isRight || p3isRight) {

System.out.println ("We have a winner!");
System.out.println ("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game over!");
break;
} else {
System.out.println("Players will have to try again.");
}
}
}
}

public class Player {
int number = 0;
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}
public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
C:\>javac GameLauncher.java
GameLauncher.java:1: class GuessGame is public, should be declared in a file nam
ed GuessGame.java
public class GuessGame {
^
GameLauncher.java:65: class Player is public, should be declared in a file named
Player.java
public class Player {
^
GameLauncher.java:41: incompatible types
found : int
required: boolean
if (guessp2 = targetNumber) {
^
GameLauncher.java:44: incompatible types
found : int
required: boolean
if (guessp3 = targetNumber) {
^
4 errors
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the error messages say, the public classes should each be in a file named after the class. And on lines 41 and 44, the "=" should be "==", a double equals sign. "=" is assignment (changing a variable) while "==" is comparison (comparing a variable to another one without changing it.)
 
Siki Rikishi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I've fixed lines 41 and 44, but what about the other errors that you mentioned. Do you mean create a new file (separately?) or is this something within this code? I'm not sure what needs to be done. Here's the error now, by the way:
GameLauncher.java:1: class GuessGame is public, should be declared in a file nam
ed GuessGame.java
public class GuessGame {
^
GameLauncher.java:65: class Player is public, should be declared in a file named
Player.java
public class Player {
^
2 errors
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message "class GuessGame is public, should be declared in a file nam
ed GuessGame.java" means (not surprisingly) that, since class GuessGame is public, it needs to be declared in its own file named "GuessGame.java". Take the whole definition of "GuessGame" and move it into a file by that name. Do likewise with the other class that gives this error.
The rule is that there can be at most one public class in a source file. Having the file be named after the class is optional, although if you don't do it that way, the Java compiler won't necessarily know where to look for the class!
[ February 01, 2004: Message edited by: Ernest Friedman-Hill ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put public class GuessGame into a file named GuessGame.java and public class Player into a file named Player.java

Another option is to remove the "public" modifier from those two classes.
 
Siki Rikishi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not working. Do I need to compile each file? When I do, I'm getting errors. In other words, what belongs in each file? How many total files will I need? I've got GameLauncher.java, GuessGame.java, and Player.java but I'm not sure what needs to be in which file. Thanks for the feedback so far and sorry for my confusion (newbie here).
 
Siki Rikishi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did it! I just saw Marilyn's reply after my previous reply and removing Public fixed it. Thanks and if someone could help me with my questions about the different files I'd appreciate it. Thanks again, everyone! :-)
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the following error message:
C:\>javac GameLauncher.java
GameLauncher.java:1: class GuessGame is public, should be declared in a file nam
ed GuessGame.java
public class GuessGame {
^
Looks like you have a class called "GuessGame" but it is in a file called "GameLauncher.java".
Try renaming the file GameLauncher.java to GuessGame.java.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic