我想创建一个数字游戏,用户输入一个1-100的数字,然后脚本将尝试猜测用户输入的10个猜测。如果在10个猜测中它猜测它赢得的正确数字,否则用户获胜。到目前为止,我得到了正常的工作,除了我有问题试图让它显示在游戏结束时猜测的数量。例如,如果进行10次猜测,则应显示“猜测数量:10”或猜测次数:5“(如果猜到5次)。
window.guessNum = function () {
document.getElementById("result").innerHTML = "";
var guesses = 10;
var input = parseInt(document.getElementById("numberGuess").value);
do {
var randomNum = Math.floor(Math.random() * 100) + 1;
document.getElementById("result").innerHTML += "I'll guess " + randomNum + " <br />";
if (input == randomNum) {
document.getElementById("result").innerHTML += "Our numbers match. I win!! :)<br /><br />";
guesses = 0;
}
if (input < randomNum) {
document.getElementById("result").innerHTML += "Your input is less than the number I'm thinking about. <br /><br />";
} else if (input > randomNum) {
document.getElementById("result").innerHTML += "Your input is greater than the number I'm thinking about.<br /><br />";
}
guesses = guesses - 1;
}
while (guesses > 0);
if (input != randomNum) {
document.getElementById("result").innerHTML += "I can't figure it out. I guess you win!<br /><br />";
}
document.getElementById("result").innerHTML += "Number of guesses: " +guesses+ " <br />";
}
<h1>Number Game</h1>
<div id="mainCont">
<p>Number:
<input type="text" id="numberGuess">
<input type="button" onclick="guessNum();" value="Guess">
</p>
<div id="guess"></div>
<div id="result"></div>
</div>
答案 0 :(得分:0)
您正在打印所留下的猜测量,而不是猜测的数量。
答案 1 :(得分:0)
您正在打印的计数器是您倒计数所猜测的数量的计数器。而不是尝试打印所做的猜测量,而是添加另一个计数器,在其中增加每个do
重复的值,然后在完成后打印它。
在以下三个地方添加了guessesMade
:
window.guessNum = function () {
document.getElementById("result").innerHTML = "";
var guesses = 10;
var guessesMade = 0;
var input = parseInt(document.getElementById("numberGuess").value);
do {
guessesMade++;
var randomNum = Math.floor(Math.random() * 100) + 1;
document.getElementById("result").innerHTML += "I'll guess " + randomNum + " <br />";
if (input == randomNum) {
document.getElementById("result").innerHTML += "Our numbers match. I win!! :)<br /><br />";
guesses = 0;
}
if (input < randomNum) {
document.getElementById("result").innerHTML += "Your input is less than the number I'm thinking about. <br /><br />";
} else if (input > randomNum) {
document.getElementById("result").innerHTML += "Your input is greater than the number I'm thinking about.<br /><br />";
}
guesses = guesses - 1;
}
while (guesses > 0);
if (input != randomNum) {
document.getElementById("result").innerHTML += "I can't figure it out. I guess you win!<br /><br />";
}
document.getElementById("result").innerHTML += "Number of guesses: " +guessesMade+ " <br />";
}
jsfiddle http://jsfiddle.net/dpbkv6pp/
答案 2 :(得分:0)
当匹配发生时,break
可以loop
:
window.guessNum = function () {
document.getElementById("result").innerHTML = "";
var guesses = 10;
var input = parseInt(document.getElementById("numberGuess").value);
do {
guesses = guesses - 1;
var randomNum = Math.floor(Math.random() * 100) + 1;
document.getElementById("result").innerHTML += "I'll guess " + randomNum + " <br />";
if (input == randomNum) {
document.getElementById("result").innerHTML += "Our numbers match. I win!! :)<br /><br />";
break;
}
if (input < randomNum) {
document.getElementById("result").innerHTML += "Your input is less than the number I'm thinking about. <br /><br />";
} else if (input > randomNum) {
document.getElementById("result").innerHTML += "Your input is greater than the number I'm thinking about.<br /><br />";
}
}
while (guesses > 0);
if (input != randomNum) {
document.getElementById("result").innerHTML += "I can't figure it out. I guess you win!<br /><br />";
}
document.getElementById("result").innerHTML += "Number of guesses: " +(10-guesses)+ " <br />";
}
<h1>Number Game</h1>
<div id="mainCont">
<p>Number:
<input type="text" id="numberGuess">
<input type="button" onclick="guessNum();" value="Guess">
</p>
<div id="guess"></div>
<div id="result"></div>
</div>