我正在完成我的入门CS课程的作业。对于此赋值,我们必须修复javascript函数,以便它提取变量的值,初始化计数器,并生成一个循环,直到找到某个数字为止,循环计数将继续增加。我的问题是,我的代码版本似乎不起作用。
对于下面我将详细描述的当前版本,我从每个'num‘文本框中提取变量,并开始构造For -循环操作,在这个操作中,每当产生一个4位数的数字(不是提取的'num’文本框中的值)时,计数就会增加。我对相同的代码进行了几次测试,即使条件语句中的“count”变量更改为“i”变量,但没有任何工作。
我正在使用random.js库--在这里找到( http://balance3e.com/random.js )--将循环的‘选择’随机化。
我还尝试过其他循环版本,包括普通的while循环和do/while-循环。
我的教授没有充分地教授这些材料,而且显得“太忙”而无法提供帮助,所以如果有些代码是完全错误的,我很抱歉。我试过了。
<!DOCTYPE html>
<html>
<head>
<title>PICK-4 Lotto</title>
<script type="text/javascript" src="random.js"> </script>
<script type="text/javascript">
function DrawUntilWinner()
// Assumes: user has entered 4 numbers in pick boxes
// Results: repeatedly generates pick-4 winners until match user pick
{
var num1, num2, num3, num4, pick1, pick2, pick3, pick4, count;
num1 = parseFloat(document.getElementById('num1').value);
num2 = parseFloat(document.getElementById('num2').value);
num3 = parseFloat(document.getElementById('num3').value);
num4 = parseFloat(document.getElementById('num4').value);
for (count = 1; count > 0; count++) {
pick1 = RandomInt(0, 9);
pick2 = RandomInt(0, 9);
pick3 = RandomInt(0, 9);
pick4 = RandomInt(0, 9);
if (pick1 != num1 || pick2 != num2 || pick3 != num3 || pick4 != num4) {
count ++;
}
}
document.getElementById('outputDiv').innerHTML = 'The number of picks needed to get '
+ num1 + '-' + num2 + '-' + num3 + '-' + num4 + ' was ' + count;
}
</script>
</head>
<body>
<div style="text-align: center">
<h2>Pick-4 Lotto</h2>
<p>
This page demonstrates the futility of lotteries. <br> Click on
the button to perform LOTTO drawings until <input type="text"
id="num1" size=1 value=0> <input type="text" id="num2"
size=1 value=0> <input type="text" id="num3" size=1 value=0>
<input type="text" id="num4" size=1 value=0> appears.
</p>
<input type="button" value="Click to begin drawing"
onclick="DrawUntilWinner()">
<hr>
<div id="outputDiv"></div>
</div>
</body>
</html>正确的代码应该显示如下所示:https://i.imgur.com/1oW4Pau.jpg
但是,不是水平规则下的“0”结尾是'0',而是在循环之后得到提取的4位数字所需的总“计数”。
感谢您的所有帮助!
发布于 2019-04-28 21:48:04
试着替换
for (count = 1; count > 0; count++) {
pick1 = RandomInt(0, 9);
pick2 = RandomInt(0, 9);
pick3 = RandomInt(0, 9);
pick4 = RandomInt(0, 9);
if (pick1 != num1 || pick2 != num2 || pick3 != num3 || pick4 != num4) {
count ++;
}
}就像这样:
count = 0;
do {
pick1 = RandomInt(0, 9);
pick2 = RandomInt(0, 9);
pick3 = RandomInt(0, 9);
pick4 = RandomInt(0, 9);
count++;
} while (pick1 != num1 || pick2 != num2 || pick3 != num3 || pick4 != num4);发布于 2019-04-28 21:44:55
问题是,您从来没有退出您的for循环,所以您的程序只是循环永远。
当您找到正确的匹配时,您应该退出for循环,如下所示:
编辑:,正如@trognander所指出的,您正在两次递增counter:一次在for循环声明中,另一次在if语句中。
for (count = 1; count > 0; count++) {
pick1 = RandomInt(0, 9);
pick2 = RandomInt(0, 9);
pick3 = RandomInt(0, 9);
pick4 = RandomInt(0, 9);
if (pick1 == num1 || pick2 == num2 || pick3 == num3 || pick4 == num4) {
break;
}
}P.S.:在您的场景中,您可以考虑使用while循环,因为只要满足条件,就可以循环,而不是固定次数的迭代:
var counter = 1;
while(pick1 != num1 || pick2 != num2 || pick3 != num3 || pick4 != num4) {
//as long as the number does not match, keep looping
counter++;
//re-pick random numbers
}https://stackoverflow.com/questions/55894763
复制相似问题