我正在完成我的Intro 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位数字所花费的总“计数”。
谢谢您的协助!
答案 0 :(得分:1)
尝试更换
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);
答案 1 :(得分:0)
问题在于您永远不会退出for循环,因此您的程序将永远循环下去。
当您找到正确的匹配项时,您应该退出for循环,如下所示:
编辑:另外,正如@trognanders所指出的,您要递增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
}