1

一个简单的直接游戏用户输入 6 个数字并单击按钮播放,如果所有 6 个数字都匹配他们中奖,我已经设置了循环,以便它不断生成随机数直到用户获胜,然后通知他们循环数它需要获得中奖号码。我的逻辑问题是,即使匹配一个数字,循环也会终止,我需要它继续运行,直到所有数字都匹配。这是代码,请帮忙,以便我可以倾斜:)

 <html>
 <head>
 <title>Lottery Game</title>
 <SCRIPT LANGUAGE="javascript">

 function genNums(){

 var counter = 0;

    do{

    var num1 = Math.round(59 * Math.random())+1
    var resultnum1 = document.lottery.Rball1.value = num1

    var num2 = Math.round(59 * Math.random())+1
    var resultnum2 =document.lottery.Rball2.value = num2

    var num3 = Math.round(59 * Math.random())+1
    var resultnum3 =document.lottery.Rball3.value = num3

    var num4 = Math.round(59 * Math.random())+1
    var resultnum4 =document.lottery.Rball4.value = num4

    var num5 = Math.round(59 * Math.random())+1
    var resultnum5 =document.lottery.Rball5.value = num5

    var num6 = Math.round(59 * Math.random())+1
    var resultnum6 =document.lottery.Rball6.value = num6

    var enterednum1 = document.lottery.ball1.value;
    var enterednum2 = document.lottery.ball2.value;
    var enterednum3 = document.lottery.ball3.value;
    var enterednum4 = document.lottery.ball4.value;
    var enterednum5 = document.lottery.ball5.value;
    var enterednum6 = document.lottery.ball6.value;

    counter = counter + 1;

}while(resultnum1 != enterednum1 && resultnum2 != enterednum2 && resultnum3 != enterednum3 && resultnum4 != enterednum4 && resultnum5 != enterednum5 && resultnum6 != enterednum6)


//alert(counter);

if(resultnum1 == enterednum1 && resultnum2 == enterednum2 && resultnum2 == enterednum2 && resultnum2 == enterednum2 && resultnum2 == enterednum2 && resultnum2 == enterednum2){

    alert("JackPot!, You Won The Lottery");
    alert("It took " + counter + " number of loops to get wining numbers")
    document.bgColor = "lightblue";

}
else{
    alert("sorry you did not win" + counter);
}


 }

</SCRIPT>



</head>
<body>

<Form Name = "lottery">

Lottery Game<br/>

Please enter your six numbers below:<br/>

Ball#1 <Input Type = "text" size = "1" value = "7" name = "ball1"><br/>
Ball#2 <Input Type = "text" size = "1" value = "45" name = "ball2"><br/>
Ball#3 <Input Type = "text" size = "1" value = "29" name = "ball3"><br/>
Ball#4 <Input Type = "text" size = "1" value = "10" name = "ball4"><br/>
Ball#5 <Input Type = "text" size = "1" value = "5" name = "ball5"><br/>
Ball#6 <Input Type = "text" size = "1" value = "25" name = "ball6"><br/>


<Input Type = "button" value = "Play" onClick = "genNums()"><br/>

Results:<br/>

Result Ball#1 <Input Type = "text" size = "1" name = "Rball1"><br/>
Result Ball#2 <Input Type = "text" size = "1" name = "Rball2"><br/>
Result Ball#3 <Input Type = "text" size = "1" name = "Rball3"><br/>
Result Ball#4 <Input Type = "text" size = "1" name = "Rball4"><br/>
Result Ball#5 <Input Type = "text" size = "1" name = "Rball5"><br/>
Result Ball#6 <Input Type = "text" size = "1" name = "Rball6"><br/>


</Form>
</body>
</html>
4

2 回答 2

3

您需要在 while 循环中有 OR 条件

while(resultnum1 != enterednum1 || resultnum2 != enterednum2 || resultnum3 != enterednum3 || resultnum4 != enterednum4 || resultnum5 != enterednum5 || resultnum6 != enterednum6)

这样,即使一组不匹配,代码也会再次循环!

而且由于您使用的是 Math.random(),因此您将很难验证逻辑,因为您可能需要预测 Math.random() 的结果,很幸运能赢得大奖:)

于 2013-03-01T20:52:06.323 回答
0

有时阅读 NOT 逻辑会有点麻烦,尤其是 while 子句依赖于某些东西是真实的来继续继续

为了便于阅读,我尽量让事情保持“真实”

} while( !(resultnum1 == enterednum1 && resultnum2 == enterednum2 && resultnum3 == enterednum3 &&
resultnum4 == enterednum4 && resultnum5 == enterednum5 && resultnum6 == enterednum6) )

编辑:另外,您的 if 语句看起来是复制和粘贴的.. 可能需要修复语句的其余部分

if ( resultnum1 == enterednum1 && resultnum2 == enterednum2 && resultnum3 == enterednum3 && resultnum4 == enterednum4 && resultnum5 == enterednum5 && resultnum6 == enterednum6 ){

编辑2:这是对我有用的代码。它大约在 4 次尝试中起作用,但这只是因为我将随机最大值减少到 6

 <html>
 <head>
 <title>Lottery Game</title>
 <SCRIPT LANGUAGE="javascript">

 function genNums(){

 var counter = 0;

    do{

    var num1 = Math.round(5 * Math.random())+1
    var resultnum1 = document.lottery.Rball1.value = num1

    var num2 = Math.round(5 * Math.random())+1
    var resultnum2 = document.lottery.Rball2.value = num2

    var num3 = Math.round(5 * Math.random())+1
    var resultnum3 = document.lottery.Rball3.value = num3

    var num4 = Math.round(5 * Math.random())+1
    var resultnum4 = document.lottery.Rball4.value = num4

    var num5 = Math.round(5 * Math.random())+1
    var resultnum5 = document.lottery.Rball5.value = num5

    var num6 = Math.round(5 * Math.random())+1
    var resultnum6 = document.lottery.Rball6.value = num6

    var enterednum1 = document.lottery.ball1.value;
    var enterednum2 = document.lottery.ball2.value;
    var enterednum3 = document.lottery.ball3.value;
    var enterednum4 = document.lottery.ball4.value;
    var enterednum5 = document.lottery.ball5.value;
    var enterednum6 = document.lottery.ball6.value;

    counter = counter + 1;

}while( !((resultnum1 == enterednum1 && resultnum2 == enterednum2 && resultnum3 == enterednum3 && resultnum4 == enterednum4 && resultnum5 == enterednum5 && resultnum6 == enterednum6 ) || counter >= 10000 ) )


//alert(counter);

if( resultnum1 == enterednum1 && resultnum2 == enterednum2 && resultnum3 == enterednum3 && resultnum4 == enterednum4 && resultnum5 == enterednum5 && resultnum6 == enterednum6) {

    alert("JackPot!, You Won The Lottery");
    alert("It took " + counter + " number of loops to get winning numbers")
    document.bgColor = "lightblue";

}
else{
    alert("sorry you did not win after " + counter + " tries");
}


 }

</SCRIPT>



</head>
<body>

<Form Name = "lottery">

Lottery Game<br/>

Please enter your six numbers below:<br/>

Ball#1 <Input Type = "text" size = "1" value = "1" name = "ball1"><br/>
Ball#2 <Input Type = "text" size = "1" value = "2" name = "ball2"><br/>
Ball#3 <Input Type = "text" size = "1" value = "3" name = "ball3"><br/>
Ball#4 <Input Type = "text" size = "1" value = "4" name = "ball4"><br/>
Ball#5 <Input Type = "text" size = "1" value = "5" name = "ball5"><br/>
Ball#6 <Input Type = "text" size = "1" value = "6" name = "ball6"><br/>


<Input Type = "button" value = "Play" onClick = "genNums()"><br/>

Results:<br/>

Result Ball#1 <Input Type = "text" size = "1" name = "Rball1"><br/>
Result Ball#2 <Input Type = "text" size = "1" name = "Rball2"><br/>
Result Ball#3 <Input Type = "text" size = "1" name = "Rball3"><br/>
Result Ball#4 <Input Type = "text" size = "1" name = "Rball4"><br/>
Result Ball#5 <Input Type = "text" size = "1" name = "Rball5"><br/>
Result Ball#6 <Input Type = "text" size = "1" name = "Rball6"><br/>


</Form>
</body>
</html>
于 2013-03-01T21:13:28.580 回答