我想做的是制作一个简单的猜谜游戏,用户可以无限制地使用任何数字,但是当根据他们的猜测数量正确猜出数字时,将在最后评分。但是,当我输入一个数字并按下按钮多次时,我的代码会从"更高"到"降低"即使数字没有改变,也没有显示正确猜测数字时应显示的消息。这是我的代码,我是初学者,因此必然会出现代码中的错误,并感谢任何帮助。
<fieldset>
<input type="number" id="guess" />
<button onClick="checknum();">Check Number</button>
</fieldset>
<fieldset>
<p>Your current status:</p>
<output id="status_output">You have yet to guess anything.</output>
</fieldset>
<script type="text/javascript">
function checknum(){
var randomNumber = Math.floor((Math.random() * 100) + 1);
var guessNumber = document.getElementById("guess").value;
//var guessNumber = parseInt(guess.value);
var statusOutput = document.getElementById('status_output');
var counter = 0;
var isguessed = false;
do {
counter = (counter + 1)
if (guessNumber < randomNumber) {
statusOutput.value = ("Higher");
}
else if (guessNumber > randomNumber) {
statusOutput.value = ("Lower");
}
else if (guessNumber = randomNumber) {
set (isguessed = true());
statusOutput.value = ("Correct" + mark());
}
}
while (isguessed = false);
}
function mark(){
if (counter < 10){
statusOutput.value("Excellent");
}
else if (counter > 10 && counter <20){
statusOutput.value("Okay");
}
else
statusOutput.value("Needs Practice");
}
</script>
答案 0 :(得分:0)
在您为名为isguessed的变量赋值false时。
您希望改为while (isguessed === false)
。这将检查isguessed是否设置为false
isguessed = false
:指定varibles isguessed为false
isguessed === false
:布尔表达式返回true或false