【问题标题】:using logic by looping通过循环使用逻辑
【发布时间】:2021-01-17 00:44:13
【问题描述】:

我正在尝试制作这个迷你猜谜游戏。用户必须猜数字。 3 次尝试后,系统会询问用户是否要继续。我已经编写了逻辑来提出 3 个问题,并提示用户决定是继续还是终止程序。但是我正在努力做到这一点,如果用户继续玩,他会继续玩。

我在想一个介于 1 和 10 之间的数字,它是什么?> 3 10 你的猜测太低了 我在想一个介于 1 和 10 之间的数字,它是什么?> 2 10 你的猜测太低了 我在想一个介于 1 到 10 之间的数字,它是什么?> 5 6 你的猜测太低了 您要继续是还是否?(y 表示是,n 表示否)> y 我在想一个介于 1 和 10 之间的数字,它是什么?> 3 1 你的猜测太高了 你要继续yes还是no?(y代表yes,n代表no)>

是或否问题不应该出现在第三个问号之前。任何人都有任何想法或知道应该使用什么循环来朝着正确的方向前进

这些是上面的结果:

function game() {
    let numOfGuesses=0;
    let inputNum;
    
    while(numOfGuesses <= 10) {
    var userGuess = prompt("I'm thinking of a number between 1 and 10, what is it?");
    
    var randomNum = Math.floor(Math.random() * 10) + 1;
    
    let number = parseInt(userGuess);
    
    numOfGuesses++;
    console.log(randomNum)
    var quit;
    
    //logic to check which are correct and not correct
    if (userGuess < randomNum) {
        alert("your guess is too low")
    }else if(userGuess > randomNum) {
        alert("your guess is too high")
    }else {
        alert("CORRECT!")
    }
    
    if (numOfGuesses >= 3 || numOfGuesses == 3) {
        quit = prompt("would you like to continue yes or no?(y for yes, n for no)")
        //break;
        if(quit == "y") {
            continue;
        } else {
            return;
        }
    }
    
    
    } 
}
game();

【问题讨论】:

    标签: javascript loops while-loop


    【解决方案1】:

    我会以不同的方式将它分开,创建一个 askNumber(actualNumber) 函数来打印问题,然后根据用户的猜测打印响应,这样我们就可以无限期地调用这个函数,直到用户得到它(也就是说,如果他们想要继续玩)

    那你说要循环三遍,我们可以试试这个

    var randomNum = Math.floor(Math.random() * 10) + 1;
    function askNumber(actualNumber) {
        var correctGuess = false;
        var userGuess = parseInt(prompt("I'm thinking of a number between 1 and 10, what is it?"));
    
        //logic to check which are correct and not correct
        if (userGuess < actualNumber) {
            alert("your guess is too low");
        }else if(userGuess > actualNumber) {
            alert("your guess is too high");
        }else {
            alert("CORRECT!");
            correctGuess = true;
        }
        return correctGuess;
    
    }
    for (var j = 0; j < 10; j++) {
        //If it is after the user's 3rd or 6th turn
        if (j == 3 || j == 6) {
            if (!confirm("Do you still want to keep playing?")) {
                break;
            }
        }
        if (askNumber(randomNum) == true) {
            //Now you can refresh the website to play again
            window.location.reload();
        }
    }
    

    【讨论】:

    • 我正在尝试 if (j == 3 || j == 6) { if (!confirm("你还想继续玩吗?")) { break;你能解释一下这个 if 语句是如何工作的吗?
    • 它询问用户是否要继续使用确认框,如果答案为“ok”,则返回 true,for 循环不会中断,但如果他们点击取消,则它返回 false 并且 for 循环中断,有效地结束了你的游戏。然后,您可以添加一个脚本来告诉用户实际的正确数字是多少
    • 那么为什么 randomNum 变量在函数内部不起作用?
    • 我意识到,在我编写代码时,它确实如此,但为了函数的缘故,我决定继续使用一个参数。但你是对的!没有它也能正常工作
    【解决方案2】:

    定义一个计数器并为每个循环找到它的模式。它将为每 3 步给出 0

    function game() {
        let numOfGuesses=0;
        let inputNum;
        let counter = 1;
        while(numOfGuesses <= 10) {
            var userGuess = prompt("I'm thinking of a number between 1 and 10, what is it?");
            
            var randomNum = Math.floor(Math.random() * 10) + 1;
            
            let number = parseInt(userGuess);
            
            numOfGuesses++;
            console.log(randomNum)
            var quit;
            
            //logic to check which are correct and not correct
            if (userGuess < randomNum) {
                alert("your guess is too low")
            }else if(userGuess > randomNum) {
                alert("your guess is too high")
            }else {
                alert("CORRECT!")
            }
            
            if (counter % 3==0) {
                quit = prompt("would you like to continue yes or no?(y for yes, n for no)")
                //break;
                if(quit == "y") {
                    continue;
                } else {
                    return;
                }
            }
            counter++
        }
    }
    

    【讨论】:

    • 我试过了,结果一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2011-06-05
    • 2019-01-08
    • 2012-03-25
    相关资源
    最近更新 更多