Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Help with Guessing Game (Head First Java)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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! :-)
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I know it's been a few years for this post. But doing a search, I ended up getting here.

Solution for using public classes:

You will need to create three (.java) files:

Player.java
GuessGame.java
GameLaucher.java

What's inside each file:


Player.java


----------------------------------------------------------------------------------------------------------------------

GuessGame.java





----------------------------------------------------------------------------------------------------------------------

GameLauche.java



-----------------------------------------------------------------------------------------------------

After that, go to the directory where the .java files are and run:

javac Player.java
javac GuessGame.java
javac GameLaucher.java



After creating the .class files run

java GameLaucher


result:

O numero a advinhar ?? 4
O jogador um forneceu o palpite 8
O jogador dois forneceu o palpite 0
O jogador tres forneceu o palpite 2

Os jogadores terao que tentar novamente.
O numero a advinhar ?? 4
O jogador um forneceu o palpite 1
O jogador doi forneceu o palpite 7
O jogador tres forneceu o palpite 4

Temos um vencedor!
O jogador um acertou?false
O jogador dois acertou?false
O jogador tres acertou?true

Fim do jogo.


--------------------------------------------------------------------------------------------------
Just change the phrases in Portuguese to English.

I hope you have people studying java.

my english is not very good,
Best regards


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Welcome to the Ranch.

In the Beginners forum we much prefer to see advice being given rather than solutions being handed out as explaining how to solve the problem and getting the OP to write the code is the best way for the OP to learn. Normally full code solutions would be deleted from the thread but as this thread is 12 years old there's no harm done so I will leave it in place.

Your English is very good and we look forward to seeing you helping out here in the future.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic