• Post Reply 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

Cannot able to get the Output

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done this problem from "Head First Java" Chapter 2 - Page 39.
I am not getting the out put.
-------------------------------
I am getting the Error Below:

C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\ClassesandObjects2007\GameLauncher.java:1: class GuessGame is public, should be declared in a file named GuessGame.java
public class GuessGame
^
C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\ClassesandObjects2007\GameLauncher.java:74: class Player is public, should be declared in a file named Player.java
public class Player
^
C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\ClassesandObjects2007\GameLauncher.java:32: cannot find symbol
symbol : method prinln(java.lang.String)
location: class java.io.PrintStream
System.out.prinln("Player one guessed" + guessp1);
^
3 errors

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

// The coding is below:


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 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.prinln("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 theee got it right" + p3isright);
System.out.println("Game is 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();
}
}


Cheers

Zakir zaknet77@hotmail.com
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java you can have only one public class in a source code file. So remove public class from GuessGame and Player.

Error 3 is a typo. It should have been



I hope this helps
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe it or not, those error messages tell you a lot. You should learn to read them and understand what they mean.

the first two tell you EXACTLY what you need to do: "class GuessGame is public, should be declared in a file named GuessGame.java"

in other words: Since the class GuessGame has the 'public' modifier in front of it, you must create a second, separate file called 'GuessGame.java', and put all the code for THAT class there.


The third error messages says "i have no idea what you are talking about. the specific thing i don't understand is this symbol: prinln(java.lang.String) - oh, and it's some kind of method."

you can then search your code for that symbol (although it also tells you it's line 32). often it's either a typo or you don't have an import correct. in this case, it's pretty obvious you meant println.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic