我是Java的新手,并试图通过做一些我在网上找到的练习来学习。如果这太天真,请原谅我。
这个练习是关于用以下规则编写掷骰子游戏的程序:
在掷骰子游戏中,传球线下注如下:两个六面骰子 冷轧;掷骰子的第一骰子被称为“滚出来”。 7或11的自动输出会自动获胜,并且会出现2,3或12的输出 自动输了。如果在出来的卷筒上滚动4,5,6,8,9或10,那么这个数字 成为“重点”。玩家继续掷骰子直到7点或者点 卷起。如果该点首先滚动,则玩家赢得下注。如果首先滚动7, 然后球员输了。 使用这些规则编写一个模拟掷骰子游戏的程序,无需人工 输入。该计划不应该要求下注,而应该计算是否 球员会赢或输。该程序应该模拟滚动两个骰子和 计算总和。添加循环以使程序播放10,000个游戏。加 计算玩家获胜次数的次数,以及玩家获胜的次数 球员输了。在10,000场比赛结束时,计算获胜的概率 [即Wins /(胜利+损失)]并输出该值。从长远来看,谁 是赢得大多数比赛,你还是房子?
这是我写的代码:
// GAME OF CRAPS
public static void main (String[] args)
{
int dice1 = 0;
int dice2 = 0;
int scorenew = 0;
int point = 0;
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++)
{
System.out.println ("roll the dices");
int score = roll (dice1, dice2);
System.out.println ("\n score " + score);
if (score == 11 || score == 7)
{
System.out.println ("\n Score = " + score);
System.out.println ("you win");
wins = wins + 1;
}
if (score == 2 || score == 3 || score == 12)
{
System.out.println ("\n Score = " + score);
System.out.println ("you lose");
loss = loss + 1;
}
else if (score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10)
{
point = point + score;
System.out.println ("\n Point = " + point);
do
{
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point)
{
System.out.println ("\n you win");
wins = wins + 1;
point = 0;
break;
}
if (scorenew == 7)
{
System.out.println ("\n you lose");
point = 0;
loss = loss + 1;
break;
}
} while (scorenew != point || scorenew != 7);
}
}
System.out.println ("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll (int d1, int d2)
{
Random randomGenerator = new Random ();
int dice1 = randomGenerator.nextInt (6) + 1;
int dice2 = randomGenerator.nextInt (6) + 1;
System.out.println ("\n dice1 = " + dice1 + " dice2 = " + dice2);
int score = dice1 + dice2;
return score;
}
每次我运行代码时,do-while条件首先被执行,所以请任何人帮我弄清楚我哪里出错了?
答案 0 :(得分:1)
do {
} while(scorenew!=point || scorenew != 7);
这个条件总是为真,所以你有一个无限循环。另外,为什么要将d1
和d2
传递到roll()
函数中?它们完全未使用且不需要。
答案 1 :(得分:1)
同时正在做你应该期望的事情。首先执行主体然后评估条件以查看它是否应该再次运行。你实际上并不需要做什么,但是你想要运行,直到其中一个条件让你摆脱了while循环。
else {
point = score;
System.out.println ("\n Point = " + point);
while (true) {
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point) {
System.out.println ("\n you win");
wins = wins + 1;
break;
}
if (scorenew == 7) {
System.out.println ("\n you lose");
loss = loss + 1;
break;
}
}
}
喜欢@Lee Daniel Crocker说你不需要将dice1和dice2传递给roll功能。
public static int roll() {
Random randomGenerator = new Random();
int dice1 = randomGenerator.nextInt(6) + 1;
int dice2 = randomGenerator.nextInt(6) + 1;
System.out.println("\n dice1 = " + dice1 + " dice2 = " + dice2);
return dice1 + dice2;
}
另一个可能有用的方法是不在方法顶部声明所有变量。你不需要重新分数或者指出第三个条件之外的东西,事实上你不需要重新分数,因为你有点:
public static void main(String[] args) throws java.lang.Exception {
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++) {
System.out.println("roll the dices");
int score = roll();
System.out.println("\n score " + score);
if (score == 7 || score == 11) {
System.out.println("\n Score = " + score);
System.out.println("you win");
wins = wins + 1;
} else if (score == 2 || score == 3 || score == 12) {
System.out.println("\n Score = " + score);
System.out.println("you lose");
loss = loss + 1;
} else {
int point = score;
System.out.println("\n Point = " + point);
while (true) {
score = roll();
System.out.println("\n Score new = " + score);
if (score == point) {
System.out.println("\n you win");
wins = wins + 1;
break;
}
if (score == 7) {
System.out.println("\n you lose");
loss = loss + 1;
break;
}
}
}
}
System.out.println("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll() {
...
}