gpt4 book ai didi

javascript - 如果答案不正确,如何让用户重新创建函数

转载 作者:行者123 更新时间:2023-12-02 15:03:25 26 4
gpt4 key购买 nike

我想向用户提问。向左、向右或继续直行。如果用户在 javascript prompt 方法中写入“straight”。其他 2 种方法都是错误的 - 游戏结束。游戏什么时候结束我希望用户再次制作这个故事。

var prompt = propmt("Where will you go? Left, right or continue straight?");
if (prompt === "Left") {
confirm("Game over, the tigers will eat you!");
} else if (where === "Straight") {
confirm("You've won!");
} else {
confirm("Game over, you've fallen to the river!");
}

如果游戏结束,我希望用户重试这个故事。谢谢!

链接到 JsFiddle https://jsfiddle.net/grqxc5kr/

最佳答案

像这样的东西会起作用:

function game() {
while (true) {
var prompt = prompt("Where will you go? Left, right or continue straight?");

if (prompt === "Left") {
confirm("Game over, the tigers will eat you!");
} else if (prompt === "Straight") {
confirm("You've won!");
return
} else {
confirm("Game over, you've fallen to the river!");
}
}
}

这不是理想的执行方式(请注意 while(true)),但这将使您得到最终结果。

更好的方法:

function game() {
var prompt = ""
while (prompt != "Left" || prompt != "Straight") {
prompt = prompt("Where will you go? Left, right or continue straight?");
if (prompt === "Left") {
confirm("Game over, the tigers will eat you!");
} else if (prompt === "Straight") {
confirm("You've won!");
} else {
confirm("Game over, you've fallen to the river! Let's play again!");
}
}
}

关于javascript - 如果答案不正确,如何让用户重新创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299713/

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