Looping issue

Why wont this while loop work

/*

* GuessGame.java

*

* Created on May 22, 2007, 3:24 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package guessgame;

import guessgame.Player;

/**

*

* @author

*/

publicclass GuessGame{

Player p1;

Player p2;

Player p3;

publicvoid startGame(){

int x = 0;

while (x<10){

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("The number to guess is " + targetNumber);

p1.guess();

p2.guess();

p3.guess();

guessp1 = p1.number;

System.out.println("Player one guessed " + guessp1);

guessp1 = p2.number;

System.out.println("Player two guessed " + guessp2);

guessp1 = 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 1 got it right? " + p1isRight);

System.out.println("Player 2 got it right? " + p2isRight);

System.out.println("Player 3 got it right? " + p3isRight);

System.out.println("Game is over.");

}else{

System.out.println("Players will have to try again.");

}

x = x + 1;

}

}

}

}

}

/*

* Player.java

*

* Created on May 22, 2007, 4:32 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package guessgame;

publicclass Player{

int number = 0;

publicvoid guess(){

int number = (int) (Math.random() * 10);

System.out.println("I'm Guessing " + number);

}

}

/*

* GameLauncher.java

*

* Created on May 22, 2007, 4:36 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package guessgame;

/**

*

* @author

*/

publicclass GameLauncher{

publicstaticvoid main (String[] args){

GuessGame game =new GuessGame();

game.startGame();

}

}

Can someone tell me how to fix this?

Message was edited by:

sys5

[5605 byte] By [sys5a] at [2007-11-27 5:35:01]
# 1

what exacly you want to do with the functionality in while(true) loop ....because this loop will run infinitely as its condition is set to true....either you put break condition to stop this loop or put some condition which results in your fullfillment of functionality to be done in it.

I think i have answered your question.

Nitin_Guptaa at 2007-7-12 15:03:38 > top of Java-index,Java Essentials,New To Java...
# 2

> if (p1isRight || p2isRight || p3isRight)

Look at where you put this statement. It will only be evaluated if guessp3 == targetNumber. That's at least one of your problems. Other than that, I don't know what "Why wont this while loop work" specifically means. The code does exactly what you told it to do.

warnerjaa at 2007-7-12 15:03:38 > top of Java-index,Java Essentials,New To Java...