首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机数竞猜游戏-限制竞猜次数

随机数竞猜游戏-限制竞猜次数
EN

Stack Overflow用户
提问于 2017-03-20 13:29:58
回答 2查看 1.6K关注 0票数 0

我正在尝试编写一个程序,生成一个用户猜测的随机数。它还限制了用户可以(或应该)进行的猜测次数。

代码语言:javascript
复制
     var highLimit = 5;
        var randNum = Math.floor((Math.random() * highLimit) + 1);
        var allowedGuesses = 2;
        var numGuesses = 0;

        function guessingGame() {
            if (numGuesses <= allowedGuesses) {                    

               do {
                    inputNum = document.numForm.number.value;
                    inputNum = parseInt(inputNum);


                    if (inputNum < randNum && inputNum > 1) {
                        document.numForm.gameResults.value = "Sorry, your guess was too low.";
                        numGuesses++;
                    }

                    else if (inputNum > randNum && inputNum < highLimit) {
                        document.numForm.gameResults.value = "Sorry, your guess was too high.";
                        numGuesses++;
                    }
                    else if (inputNum == randNum) {
                        document.numForm.gameResults.value = "Congratulations! You guessed correctly.";
                        numGuesses++;
                    }

                    else {
                        document.numForm.gameResults.value = "Your guess was out of the desired parameters. Please guess again.";
                        numGuesses--;
                    }

                } while(numGuesses < allowedGuesses && inputNum != randNum);

            }

                else {
                    document.numForm.gameResults.value = "Sorry, you have reached the allowed number of guesses, which is " + allowedGuesses + "." + " Click 'New Game' to try again.";
                }
            return false;
        }

        numGuesses = 0;
        function newGame() {
                randNum = Math.floor((Math.random() * highLimit) + 1);
                guessingGame();
                return false;
           }

和HTML:

代码语言:javascript
复制
 <form name = "numForm">
        <fieldset>
            <label for = "randNum">Enter a number between 1 and 5: </label>
            <input type = "text" name = "number" value = "" id = "randNum" size = "1" maxlength = "1" />
        </fieldset>
        <input class = "button" type = "button" name = "submit" onclick = "guessingGame()" value = "Submit Number"  />

        <input class = "button" type = "reset" name = "newGame" onclick = "newGame()" value = "New Game" />
        <fieldset>
            <textarea name = "gameResults"  onclick = "" readonly = "true" value = "" rows = "5" cols = "40" ></textarea>
        </fieldset>
    </form>

现在,由于highLimit设置为5,程序陷入了无限循环,但如果我将其设置为10,程序就可以工作了。我怎样才能修复它,使它在任何值下都能工作?我也会感谢其他的改进建议。

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2017-03-20 13:44:17

我认为你不需要在这个代码中使用循环。你可以简单地说:

代码语言:javascript
复制
function guessingGame() {
    if (numGuesses < 0){numGuesses=0;}
    if ((numGuesses < allowedGuesses) && (inputNum != randNum)) {                    
            inputNum = document.numForm.number.value;
            inputNum = parseInt(inputNum);


            if (inputNum < randNum && inputNum > 1) {
                document.numForm.gameResults.value = "Sorry, your guess was too low.";
                numGuesses++;
            }

            else if (inputNum > randNum && inputNum < highLimit) {
                document.numForm.gameResults.value = "Sorry, your guess was too high.";
                numGuesses++;
            }
            else if (inputNum == randNum) {
                document.numForm.gameResults.value = "Congratulations! You guessed correctly.";
                numGuesses++;
            }

            else {
                document.numForm.gameResults.value = "Your guess was out of the desired parameters. Please guess again.";
                numGuesses--;
            }
    }

        else {
            document.numForm.gameResults.value = "Sorry, you have reached the allowed number of guesses, which is " + allowedGuesses + "." + " Click 'New Game' to try again.";
        }
    return false;
}

if (numGuesses < 0){numGuesses=0;}也会有所帮助。对于否定的情况。

票数 0
EN

Stack Overflow用户

发布于 2021-07-17 20:05:02

这是的更新

运行了该代码片段,并在"0次尝试“中进行了尝试

这就是我怎么做的:但是如果4年后你仍然需要,你必须用你指定的输入和输出替换‘提示’和‘警告’:

代码语言:javascript
复制
 let maximum = parseInt(prompt('Enter the maximum number!') );
    while(!maximum) {
      maximum = parseInt (prompt('Please enter a valid number') )
    }
    const targetNum = Math.floor(Math.random()*maximum)+1;
    let attempts =0;
    let maxAttempts = Math.ceil(maximum/2);

    // ==== Uncomment the line below to cheat --> ======

    // console.log(targetNum)

    let guess = parseInt(prompt(`Enter your first guess!, you have ${maxAttempts} attempts (press 'q' anytime to escape from this hellish nightmare)`));
    while(parseInt(guess) !==targetNum && attempts < maxAttempts-1)  {
     
      if (typeof(guess) === 'string' && guess.toLowerCase() ==='q') {break;}
      attempts++;
      if(guess>targetNum) {
        guess = (prompt(`Too High! Enter new guess! attempts left: ${maxAttempts- attempts} (press 'q' anytime to escape from this hellish nightmare)`))
      }else {
        guess= (prompt(`Too low! Entere a new Guess! attempts left: ${maxAttempts- attempts} (press 'q' anytime to escape from this hellish nightmare)`))
      }
    } 
    if(typeof(guess) === 'string' && guess.toLowerCase() ==='q') {
      alert(`You gave up after ${attempts} attempts`); 
    }
    else if(attempts >= maxAttempts-1) {alert(`Oops, your ${maxAttempts} attempts ran out, the number was ${targetNum}: Verification : ${targetNum==guess}`);}
    else {
    alert(`Good job, you got it ${attempts+1} attempts!, the number was ${targetNum}: Verification : ${targetNum==guess}`) ; 
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42896427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档