简体   繁体   English

我的代码没有加载(卡在do / while循环?)

[英]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.

相关问题 Javascript代码陷入无尽的“ do while”循环中 - Javascript code stuck in endless “do while” loop 虽然函数卡在循环中,但似乎循环的一半未执行 - While function stuck in loop, seems that half of loop doesn't execute 我的 do while 循环陷入无限循环 - My do while loop gets stuck in an infinite loop 为什么将此无限循环添加到我的 javascript 代码后我的页面无法加载? - Why my page doesn't load after adding this infinite loop to my javascript code? 我制作的网页不会停止在 JavaScript 中的 while 循环(或其他原因)中运行代码,导致网页无法加载 - My web-page that I made doesn't stop running code in a while loop (or some other cause) in JavaScript causing the web-page to not load 我的javascript循环/ while语句代码不起作用,请帮助我确定我的问题 - My javascript loop/ while statement code doesn't work, please help me identify my problem 我在 jquery 上的循环代码不适用于循环内的 .each - My loop code on jquery doesn't work with .each inside the loop 我的 Javascript 代码使用了 100% 的 CPU 并且无法加载 - My Javascript code uses 100% of my CPU and doesn't load js“ while”循环不在内部执行代码 - js 'while' loop doesn't execute code inside Javascript代码在while循环内不起作用 - Javascript code doesn't work inside a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM