我很难从书中的一个练习中理解一些东西(完全初学者)。
示例代码是用三个玩家制作一个猜谜游戏,它为所有三个玩家生成一个随机 int,它必须与游戏随机生成的 int 相同。
该代码包含三个类,但这是我有问题的最重要的一个。另外两个只是 GameLauncher 类和 Player(与其他三个玩家对战的那个)类。
public class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame(){
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 10;
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 has guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player two has guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three has 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("The game is over.");
break;
}else{
System.out.println("None of you got it right! Try again!");
}
}
}
}
从上面的代码:
int guessp1 = 10;
int guessp2 = 0;
int guessp3 = 0;
是我不明白的那个。最初,它们都被分配了值 0。我试图分配 10 以查看会发生什么,但没有任何改变。比赛的结果是一样的。
我的问题是,如果有的话,分配给声明整数的值有什么意义?尤其是在这种情况下。
排除正常用途,例如打印它或用数学操作它。