gpt4 book ai didi

JavaScript,一个用于询问特定答案的循环

转载 作者:行者123 更新时间:2023-11-30 08:02:56 25 4
gpt4 key购买 nike

我希望用户输入一个 1-100 之间的数字,直到用户输入一个有效数字,循环会一直说“该输入无效”。

我的代码如下。我哪里出错了?

// Initialize var userGuess
var userGuess;

// I want to make the prompt keep asking a number between 1-100, if it doesn't satisfy the requirement, it will keep asking
for (var valid = false; valid == true;) {
userGuess = prompt("Guess a number");
if ((userGuess >= 1) && (userGuess <= 100)) {
valid = true;
} else {
valid = false;
console.log("That number is invalid! Please enter a number between 1-100");
}
}

最佳答案

// Initialize var userGuess
var userGuess;

// I want to make the prompt keep asking a number between 1-100, if it doesn't satisfy the requirement, it will keep asking
var valid = false;
while(!valid){
userGuess = prompt("Guess a number");
if ((userGuess >= 1) && (userGuess <= 100)) {
valid = true;
} else {
console.log("That number is invalid! Please enter a number between 1-100");
}
}


// Initialize var userGuess
var userGuess;

用for循环

// I want to make the prompt keep asking a number between 1-100, if it doesn't satisfy the requirement, it will keep asking
for(var valid = false; !valid){
userGuess = prompt("Guess a number");
if ((userGuess >= 1) && (userGuess <= 100)) {
valid = true;
} else {
console.log("That number is invalid! Please enter a number between 1-100");
}
}

关于JavaScript,一个用于询问特定答案的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23664757/

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