[英]My code doesn't load (stuck at do/while loop?)
I've been trying to make "a program" that basically keeps "throwing dice" (randomize the value) until you get snake eyes, and when you do it's supposed to show how many times the dice were thrown. 我一直试图制作一个“程序”,基本上保持“掷骰子”(随机化值),直到你得到蛇眼,当你这样做时,它应该显示掷骰子的次数。 However, it doesn't load.
但是,它不会加载。 What did I do wrong?
我做错了什么?
var countRolls = 0;
do {
var die1 = (Math.random() * 6) + 1;
var die2 = (Math.random() * 6) + 1;
++countRolls;
} while (die1 != 1 || die2 != 1);
console.log(countRolls);
I've tried opening it in Chrome/Firefox and it'll just load forever, and I won't be able to open the console. 我已经尝试在Chrome / Firefox中打开它,它将永远加载,我将无法打开控制台。 Tried it in JSFiddle, and it became unresponsive.
在JSFiddle中尝试过,它变得没有反应。
因为Math.random()
返回一个double ,所以die1和die2的概率几乎为0%都为1
Your loop will never end because you add one to the random function and the stop condition is just testing for not 1 but this will not happen. 你的循环将永远不会结束,因为你在随机函数中添加一个,而停止条件只是测试不是1,但这不会发生。
Add console.log(die1,die2);
添加
console.log(die1,die2);
to your do {}
到你
do {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.