gpt4 book ai didi

javascript - 如何让 javascript 提示问题一定次数

转载 作者:行者123 更新时间:2023-12-02 21:40:19 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序,要求用户猜测随机数一定次数。到目前为止,我有以下内容:

<!DOCTYPE html>
<html>
<head>
<title>Decisions and Loops</title>
</head>
<body>

</body>
<script>

var rNum = Math.ceil(Math.random() * 30);

var myInput = Number (prompt ("Please enter your Guess: "));


if (myInput == rNum) {
alert("Good guess");
}
else if (myInput >= rNum){
alert((prompt ("Sorry, guess if too low, try again"));
}
else {
alert((prompt ("Sorry, guess if too high, try again"));
}

</script>
</html>

两个问题1. 由于某种原因,这不起作用。2. 如何让程序多次循环执行?任何帮助将不胜感激。

最佳答案

var rNum = Math.ceil(Math.random() * 30);

var myInput;
var numTimesToAsk = 3;

for (var i = 0; i < numTimesToAsk; i++) {
myInput = Number (prompt ("Please enter your Guess: "));
if(myInput === rNum) {
alert("Good guess");
break; //if you want the loop to stop here since they guessed correctly
} else if (myInput >= rNum) {
alert("Sorry, guess is too low, try again");
} else {
alert("Sorry, guess is too high, try again");
}
}

关于javascript - 如何让 javascript 提示问题一定次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com