我正在制作一个模拟器,以查看随机数生成器“滚动”蛇眼所需的次数。该程序只是重复
Not snake eyes yet :(
这是代码:
import java.util.Random;
public class SnakeEyes {
public static void main(String[] args) {
Random myRandom = new Random();
int die1 = 0, tries = 0, die2 = 0;
System.out.println("Welcome to SEO (Snake Eyes Operator)!");
System.out.println("How many rolls will it take you to get to snake eyes! ");
die1 = myRandom.nextInt(6) + 1;
die2 = myRandom.nextInt(6) + 1;
while (die1 + die2 != 2) {
System.out.println("Not snake eyes yet :( ");
tries = +1;
}
System.out.print("You have rolled snake eyes! ");
System.out.print("Congratulations! It only took you ");
System.out.print(tries);
System.out.print("!");
}
}
我相信这是一个开环问题,但我不确定。谢谢!
最佳答案
使用while循环时,必须更新检查的值,否则循环将永远不会停止。
您需要在循环内更新die1
和die2
值。