作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 Java 类编写一个简单的掷骰子模拟器,由于某种原因,它无法正确运行。它应该用“point”来跟踪损失并用“point”来跟踪获胜,但由于某种原因,这些值每次都倾向于为 1 或 0。第一轮的输赢似乎正在发挥作用。想知道是否有人有新的眼光可以找出我搞砸的地方。谢谢!
import java.util.Scanner;
import java.util.Random;
class CrapsSimulator {
public static void main(String[] args) {
// Set up values we will use
int lossfirstroll = 0;
int winfirstroll = 0;
int losswithpoint = 0;
int winwithpoint = 0;
boolean gameover = false;
int point = 0;
// Loop through a craps game 100 times
for (int i = 0; i < 100; i++) {
// First roll -- random number within 2-12
Random rand = new Random();
int random = rand.nextInt(11) + 2;
// Win on first roll
if (random == 7 || random == 11) {
winfirstroll++;
gameover = true;
} // Loss on first roll
else if (random == 2 || random == 3 || random == 12) {
lossfirstroll++;
gameover = true;
} else // Player has "point"
{
point = random;
}
// Check to make sure the game hasn't ended already
while (gameover == false) {
// Reroll the dice
random = rand.nextInt(11) + 2;
// Check to see if player has won
if (random == point) {
winwithpoint++;
gameover = true;
}
// Or if the player has lost
if (random == 7) {
losswithpoint++;
gameover = true;
}
// Otherwise, keep playing
gameover = false;
}
}
// Output the final statistics
System.out.println("Final Statistics\n");
System.out.println("Games played: 100\n");
System.out.println("Wins on first roll: " + winfirstroll + "\n");
System.out.println("Losses on first roll: " + lossfirstroll + "\n");
System.out.println("Wins with point: " + winwithpoint + "\n");
System.out.println("Losses with point: " + losswithpoint + "\n");
}
}
最佳答案
要么通过调试器运行它,要么撒上System.out.println
并查看逻辑在哪里失败。这是作业吗?
关于Java 循环错误 - 需要新的眼光,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729596/
我是一名优秀的程序员,十分优秀!