這不明白guessp1和guessp2和guessp3如何分配p1.number等代碼就有人棚點亮這個?我下文稱一個例子,從書中頭扎程序名猜謎遊戲,但我明白整個程序,但某些部分不undestood
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; //this part is not understood
System.out.println(「Player one guessed 「 + guessp1);
guessp2 = p2.number; //this part is not understood
System.out.println(「Player two guessed 「 + guessp2);
guessp3 = p3.number; //this part is not understood
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.」);
break; // game over, so break out of the loop
} else {
// we must keep going because nobody got it right!
System.out.println(「Players will have to try again.」);
} // end if/else
} // end loop of loop
} // end method method end here
} // end class
我在那些我不明白的行前面添加了評論?所以請擺脫一些光線,這將是非常有幫助的完整
請正確格式化您的代碼。很痛苦,現在閱讀它。 – kolossus
Players是使用類Player的Player()對象。你有代碼嗎?顯然,當你使用player.guess()函數時,玩家會被問到屏幕上的一個數字,並將其存儲在這個玩家數字字段中 –