Java 为相同变量返回不同值的不同打印方法?
我写了一个简单的程序来模拟一个掷骰子的游戏,除了我注意到我的“记分板”会根据我打印它的方法返回不同的值之外,一切似乎都很好 使用print语句打印“wins”变量将返回正确的结果 但是通过另一个格式化字符串“status”打印“wins”变量会返回较低的值。我知道我在这里遗漏了一些东西,因为我已经很久没有编程了,但是我很困惑这是怎么发生的。非常感谢您的反馈Java 为相同变量返回不同值的不同打印方法?,java,Java,我写了一个简单的程序来模拟一个掷骰子的游戏,除了我注意到我的“记分板”会根据我打印它的方法返回不同的值之外,一切似乎都很好 使用print语句打印“wins”变量将返回正确的结果 但是通过另一个格式化字符串“status”打印“wins”变量会返回较低的值。我知道我在这里遗漏了一些东西,因为我已经很久没有编程了,但是我很困惑这是怎么发生的。非常感谢您的反馈 public class RandomSumGame { public static void main(String[] arg
public class RandomSumGame {
public static void main(String[] args) {
// TODO Auto-generated method stub\
RandomSumGame test = new RandomSumGame();
test.play();
}
boolean start;
int d1;
int d2;
int sum;
int valuePoint;
int wins;
int loss;
String status;
public void play(int d1, int d2)
{
status= String.format("printing through variable - wins : %d | loss : %d \n", wins, loss );
if (sum == 11 || sum == 7)
{
System.out.println("Natural - You Win!");
wins = wins + 1;
}
else if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Craps! - You lose!");
loss = loss + 1;
}
else {
valuePoint = sum;
System.out.printf("You set the value point of %d = %d + %d \n", valuePoint, d1, d2);
while (true) {
rollDice();
if (sum == valuePoint) {
System.out.println("YOU WIN!");
wins = wins + 1;
break;
}
else if (sum == 7) {
System.out.println("YOU LOSE!");
loss = loss + 1;
break;
}
else {
System.out.println("ROLLING DICE AGAIN!");
continue;
}
}
}
System.out.printf("Straight up printing - wins : %d | loss : %d \n", wins, loss );
System.out.println(status);
}
public void play() {
int round = 1;
start = true;
while (start == true){
System.out.println("Round : " + round);
round +=1;
rollDice();
play(d1, d2);
//System.out.println(status);
if (round >3) {
start = false;
}
}
}
public void rollDice() {
d1 = (int) (Math.random() * 6 + 1);
d2 = (int) (Math.random() * 6 + 1);
sum = d1 + d2;
System.out.printf("YOU ROLL THE DICE! - sum is: %d , Dice 1: %d , Dice 2: %d\n", sum, d1, d2);
}
}
这里是控制台中的示例输出,您可以看到它们返回不同的结果
第二轮:1
你掷骰子总和是:7,骰子1:3,骰子2:4
自然-你赢了
直接打印-赢:1 |输:0
通过变量打印-赢:0 |输:0
第二轮:2
你掷骰子总和是:11,骰子1:5,骰子2:6
自然-你赢了
直接打印-赢:2 |输:0
通过变量打印-赢:1 |输:0
第二轮:3
你掷骰子总和是:10,骰子1:4,骰子2:6
将值点设置为10=4+6
你掷骰子总和是:6,骰子1:1,骰子2:5
再次掷骰子
你掷骰子总和是:8,骰子1:6,骰子2:2
再次掷骰子
你掷骰子总和是:4,骰子1:2,骰子2:2
再次掷骰子
你掷骰子总和是:10,骰子1:6,骰子2:4
你赢了
直接打印-赢:3 |输:0
通过变量打印-wins:2 | loss:0字符串在Java中是不可变的。创建字符串后,该字符串的值永远无法更改 因此,这里您正在创建
状态
字符串,该字符串的值为wins
和loss
status = String.format("printing through variable - wins : %d | loss : %d \n",
wins, loss );
然后更改wins
值。现在,您不能期望状态
字符串值反映赢
或输
的当前值
status = String.format("printing through variable - wins : %d | loss : %d \n",
wins, loss );
您必须使用最新的值创建一个新字符串。字符串在Java中是不可变的。创建字符串后,该字符串的值永远无法更改 因此,这里您正在创建
状态
字符串,该字符串的值为wins
和loss
status = String.format("printing through variable - wins : %d | loss : %d \n",
wins, loss );
然后更改wins
值。现在,您不能期望状态
字符串值反映赢
或输
的当前值
status = String.format("printing through variable - wins : %d | loss : %d \n",
wins, loss );
您必须使用最新的值创建一个新字符串。打印前,只需将此行移到函数末尾即可。
status=String.format(“通过变量打印-wins:%d |损失:%d\n”,wins,损失)代码>
您正在使用预先处理的赢、输值创建字符串。只需在打印前将此行移到函数末尾即可。
status=String.format(“通过变量打印-wins:%d |损失:%d\n”,wins,损失)代码>
您正在使用预先处理的赢、输值创建字符串。在掷骰子之前,您将字符串值分配给状态变量。然后,赢家和输家次数将不会在状态
字符串中更新。
为了纠正movestatus=String.format(“通过变量打印-wins:%d |损失:%d\n”,wins,损失”)代码>就在System.out.println(状态)之前代码>行,如下所示
在掷骰子之前,将字符串值指定给状态变量。然后,赢家和输家次数将不会在状态
字符串中更新。
为了纠正movestatus=String.format(“通过变量打印-wins:%d |损失:%d\n”,wins,损失”)代码>就在System.out.println(状态)之前代码>行,如下所示