シンプルで単純なゲーム ユーザーが 6 つの数字を入力し、再生ボタンをクリックします。6 つの数字すべてが一致すると宝くじに当たります。ユーザーが勝つまで乱数を生成し続けるようにループを設定し、ループの回数を通知します。当選番号にたどり着くのにかかりました。ロジックに関する私の問題は、1 つの数字が一致してもループが終了することです。すべての数字が一致するまで実行し続ける必要があります。これがコードです。私が傾くことができるように助けてください:)
<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>