使用用户输入来询问玩家是否愿意再次玩。问题:在某些情况下循环

时间:2019-04-23 18:47:43

标签: javascript console

我创建了一个猜谜游戏,其中用户有6次尝试猜测数字,在这种情况下为50。在用户正确猜测数字之后,系统会提示他们问题,是否要再次玩?。如果用户输入是,程序将再次运行。问题是,当用户在玩了多个游戏后没有键入任何提示时,会多次提示他们相同的消息。[请参见底部以获得更好的示例]

我试图在while循环中放置不同的条件。我认为问题在于每次用户输入“是”时,代码循环都会进行补偿。

function start(){
    //var answer = Randomizer.nextInt(1, 100);
    var answer = 50;
    var guess;
    var lastGuess;
    var attempts = 1;

    while(guess != answer){
        guess = readInt("Guess the number. ");

        //if correct
        if(answer == guess){
            println("Correct guess, it took you " + attempts + " tries. ");
            playAgain();

        //if too high    
        }else if(answer < guess){
            if(lastGuess && answer < lastGuess){
                println("Guess was STILL too high. ");
            }else{
            println("Guess was too high. ");
            }
        lastGuess = guess;

        //if too low
        }else if(answer > guess){
            if(lastGuess && answer > lastGuess){
                println("Guess was STILL too low. ");                
            }else{
                println("Guess was to low. ");
            }
        lastGuess = guess;

        }

        //if 6 attempts done
        if(attempts == 6){
            break;
        }

    attempts++;
    }

    //end game at 6 attempts
    if(guess != answer && attempts == 6){
        println("The answer was " + answer + ". You did not guess the number. ");
        playAgain();
    }
}
function playAgain(){
    var askPlayAgain = "Yes";
    while(askPlayAgain != "No"){
        askPlayAgain = readLine("Do you want to play again? [Yes/No] ");
        if(askPlayAgain == "No"){
            break;
        }
        if(askPlayAgain == "Yes"){
            start();
        }else{
            println("Please type in your response again? ");
            playAgain();
        }
    }
}

结果:

Guess the number. 50
Correct guess, it took you 1 tries.
Do you want to play again? [Yes/No] Yes
Guess the number. 50
Correct guess, it took you 1 tries.
Do you want to play again? [Yes/No] No
game has ended
Do you want to play again? [Yes/No] No
game has ended

预期结果:

Guess the number. 50
Correct guess, it took you 1 tries.
Do you want to play again? [Yes/No] Yes
Guess the number. 50
Correct guess, it took you 1 tries.
Do you want to play again? [Yes/No] No
game has ended

如果用户键入否(是),则系统将提示用户相同次数的消息。例如,如果我玩了3次游戏,并在游戏结束后键入no,则提示将总共显示3次。

1 个答案:

答案 0 :(得分:0)

再次道歉,我之前给出的答案完全不正确(现已删除)。我相信我现在已经了解了您的问题的真正原因以及如何解决:

多次提示用户的原因是,用户先前每次说“是”一次,是因为对于每个“是”答案,您都在再次调用start()。当然,这是您重新启动游戏所需要做的-问题的根源不在于其本身,而是在start函数运行完之后(包括对{{1}的调用) }),则需要在playAgain中输入循环的另一个迭代。当然,这意味着再次询问用户。

因此,问题实际上出在playAgain内部。解决该问题的最干净方法是我想更改循环的结构方式,就像这样(几种方法中的一种):

playAgain

此循环将一直运行,并提示用户,直到用户键入function playAgain(){ var askPlayAgain; while(true){ askPlayAgain = readLine("Do you want to play again? [Yes/No] "); if(askPlayAgain == "No"){ break; } else if(askPlayAgain == "Yes"){ start(); break; }else{ println("Please type in your response again? "); } } } "No"为止。如果他们键入"Yes",则该循环将不执行任何操作而退出,因此该程序完成。并且如果他们输入"No",游戏将重新开始。至关重要的是,尽管在完成操作后会再次提示用户进行另一款游戏(即"Yes"末尾的playAgain调用),但是一旦他们说出start,就可以了,因为在用户键入"No"之后,对堆栈上playAgain的调用都无法执行任何操作。

确实,尽管我对代码进行了一些整理,但唯一有意义的更改是在调用"No"之后添加了break-这样可以防止循环再次运行。