wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Guess Game problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Guess Game problem" Watch "Guess Game problem" New topic
Author

Guess Game problem

Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
Hey guys

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);

public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}

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);
}
}

what is wrong with this in the compiler it says line 25 illegal start of expression public class GameLauncher {
^
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Please Indent your code correctly and UseCodeTags to post your code. This activity will make your work of finding the issue easier.


Mohamed Sanaulla | My Blog
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7553
    
  18

Craig E. Lewis wrote:what is wrong with this in the compiler it says line 25 illegal start of expression public class GameLauncher {
^

What Mohamed said.

However, it would appear that you're trying to define a new class inside a method, in which case it will work like a local variable so you can't put qualifiers like public on it. I'd also question whether that is, in fact, what you want to do. I've been programming Java for 11 years now, and I have yet to define a local class.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

I would have put the code tags on the post for you, but it is not properly formatted, so it wouldn't do much good.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
Akin Millone,
Your post was moved to a new topic.
 
 
subject: Guess Game problem
 
Similar Threads
GuessGame errors
Exceptation in thread "main" java.lang.NoSuchMethodError: main
Head First Java Book, Chapter 2
Please Help with Guessing Game (Head First Java)
Very new to java