sypher2
June 10th, 2015, 03:24 AM
So I'm following this book, "Head First : Java", and it covers Java 5.0. I think that may be where my problem lies, because I am trying to run this program directly from the book, and I am getting a syntax error. I understand what its saying, but im trying to figure out why this book wanted me to do it this way in the first place, and then figure out what I can do to solve this.
So here's what's happening, I run this program, and its an infinite loop. The 3 players guesses are never considered true/correct, even though they guess the same number as the random number. The game should end , but it doesnt. it stays on an infinite loop. I will post three different classes all pertaining to this game.
public class GameLauncher {
public static void main(String[] args){
GuessGame game = new GuessGame();
game.startGame();
}
}
---------------------------------------------------------------------------------------
public class Player {
int number = 0;
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("Im guessing " + number);
}
}
------------------------------------------------------------------------------------------------------------
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 is over!");
} else {
System.out.println("Players will have to try again");
}
}
}
}
So here's what's happening, I run this program, and its an infinite loop. The 3 players guesses are never considered true/correct, even though they guess the same number as the random number. The game should end , but it doesnt. it stays on an infinite loop. I will post three different classes all pertaining to this game.
public class GameLauncher {
public static void main(String[] args){
GuessGame game = new GuessGame();
game.startGame();
}
}
---------------------------------------------------------------------------------------
public class Player {
int number = 0;
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("Im guessing " + number);
}
}
------------------------------------------------------------------------------------------------------------
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 is over!");
} else {
System.out.println("Players will have to try again");
}
}
}
}