[英]JAVA: How do I add two processed values with same name declared but different values
我正在寻找一种方法将玩家 1 的两个“最终得分”相加为玩家 1 的总得分,以便稍后与其他玩家的总得分进行比较。
int player11, player12, player21, player22;
int score1;
int score2 = 0;
Random random = new Random();
int i=0;
do {
do {
System.out.println("Player 1 rolls the dice");
player11 = random.nextInt(6) + 1;
player12 = random.nextInt(6) + 1;
score1 = player11 + player12;
System.out.println(player11 + "\t" + player12 + "\t" + score1);
if (player11 == 1 && player12 == 1) {
score1 = score1 * 2;
} else if ((player11 % 2 != 0 && player12 % 2 != 0) && (player11 != 1 && player12 != 1)) {
score1 -= 5;
} else if (player11 == 6 && player12 == 6) {
System.out.println("Player 1 rerolls the dice");
score1 = 0;
}
} while (player11 == 6 && player12 == 6);
System.out.println("Final Score: " + score1);
i++;
} while (i<2);
到目前为止,我所了解的是您想在每次迭代后添加分数,对吗? 在这种情况下,您可以使用 temp 来存储计算的分数,然后在内部 do-while 循环完成后添加它。 像下面这样:
do {
int temp = 0; //New variable to temporarily save scores
do {
System.out.println("Player 1 rolls the dice");
player11 = random.nextInt(6) + 1;
player12 = random.nextInt(6) + 1;
temp = player11 + player12; //instead of score we're saving it in temp
/* your code continued.....
remember to swap score1 with temp at every place
.....
.....
*/
}
} while (player11 == 6 && player12 == 6);
score1 = score1 + temp; // Finally we add it in the score1 variable
System.out.println("Final Score: " + score1);
i++;
} while (i<2);
我希望这能解决这个微不足道的问题。 另外我认为您没有初始化可能导致问题的 score1 变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.