制作一个猜谜游戏,我需要帮助让我的猜谜游戏具有有限的猜测次数,并显示用户每次猜测后剩余的猜测数量。
我尝试创建一个 else if 语句来执行此操作,但它似乎不起作用。
function main() { const minNum=0; const maxNum=100; const answer=Math.floor(Math.random()*(maxNum-minNum+1));
let attempts=0;
let inGame=true;
let guess;
while(inGame){
guess=readInt("guess a number between 1 and 100: ");
if (guess == answer) {
inGame=false;
console.log("you have successfully found the number");
} else if (guess < answer) {
inGame=true;
console.log("your guess is too low");
} else if (guess > answer) {
inGame=true;
console.log("your guess is too high");
} else if (guess == 7) {
inGame=false;
console.log("you have failed the guess the number");
}
}
}
main();
您是否在计数器中使用自定义库?