【发布时间】:2017-08-13 19:46:28
【问题描述】:
这是我第一次在这里提问。 我是 Java 新手,我对这段代码中的循环有疑问 我不知道在哪里打破循环。
谢谢你帮助我 :) this image is from the book regarding this question
import java.util.*;
public class GameOfCraps {
public static void main(String[] args) {
Random rn = new Random();
int counterw = 0;
int counterl = 0;
int countsum = counterl + counterw;
int points = 0;
do {
int rndice1 = rn.nextInt(5) + 1; // 1 to 6
int rndice2 = rn.nextInt(5) + 1;// 1 to 6
int sum = rndice1 + rndice2;// sum of dice random
if (sum == 2 || sum == 3 || sum == 12) {
// System.out.println("you lose");
counterl++;
}
else if (sum == 7 || sum == 11) {
// System.out.println("you won");
counterw++;
}
else {
do {
boolean xc = false;
points = sum;
int rndice3 = rn.nextInt(5) + 1;
int rndice4 = rn.nextInt(5) + 1;
if (rndice3 + rndice4 == points) {
// System.out.println("you won");
counterw++;
xc = true;
//break;
}
if (xc == false)
counterl++;
} while (points != 7);
}
} while (countsum <= 10000);
System.out.println(counterw);
System.out.println(counterl);
System.out.println("probability of winning the game: "+(double)(counterw)/(counterw+counterl));
}
}
【问题讨论】:
-
这里的实际问题是什么?预期结果是什么,目前的结果是什么?在我看来,您根本不了解 do{]while() 的工作原理?
-
欢迎来到 Stack Overflow!看起来你可能正在寻求家庭作业帮助。虽然我们对此本身没有任何问题,但请注意这些dos and don'ts,并相应地编辑您的问题。 (即使这不是家庭作业,也请考虑建议。)
标签: java loops if-statement random