【问题标题】:How would I stop Javascript if it doesn't meet a specific condition?如果 Javascript 不满足特定条件,我将如何停止它?
【发布时间】:2015-04-30 20:15:53
【问题描述】:

我正在从 Codecademy 学习 Javascript,我正在研究额外的问题,看看我是否能做到。我目前正在开发剪刀石头布游戏,如果用户输入石头、纸或剪刀以外的选项,我试图让它出现错误。我设法让它拉出错误消息,但脚本将继续运行,计算机仍会选择三者之一。

如果用户输入的不是石头或剪刀,我如何让程序停止,并且只返回错误消息而不是返回消息然后继续显示计算机的选择?

var userChoice = prompt("Do you choose rock, paper or scissors?");

if  (userChoice !== "rock" || "scissors" || "paper") {
    console.log("That is not one of the options");
}

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);


var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

compare(userChoice, computerChoice)

【问题讨论】:

  • 顺便说一句,你不能比较这样的变量
  • (userChoice !== "rock" || "scissors" || "paper") 应该是(userChoice !== "rock" &amp;&amp; userChoice !== "scissors" &amp;&amp; userChoice !== "paper")

标签: javascript


【解决方案1】:

方法一

if  (userChoice !== "rock" && userChoice  !== "scissors" && userChoice  !== "paper") {
    console.log("That is not one of the options");
    return false;
}

方法二

switch(userChoice)
{
  case 'rock': break;
  case 'scissors': break;
  case 'paper': break;
  default: {
     console.log(userChoice + "That is not one of the options");
     return false;
  }
 }

在 jsfiddle 工作:enter link description here

【讨论】:

  • 我用 && 和 || 试过了均以非法返回语句返回
【解决方案2】:

如果if 语句位于函数内部,请在if 语句的末尾使用return false;。

如果if 语句在函数之外,请将其余代码包装在else 子句中,这样如果初始条件成功,它将不会执行。

【讨论】:

  • 这做得很完美。我正在尝试返回 false,但它一直给我错误。我意识到这是因为它不是一个函数。一旦我添加了 else 子句,它就完美地修复了它。非常感谢。
【解决方案3】:

您可以使用异常,如下所示:Is it possible to stop JavaScript execution?

基本上, 触发错误消息后,请执行以下操作:

throw new Error("Your Error Message!");

虽然这会结束整个游戏,但对于用户的拼写错误是不可取的。它会有效地停止程序。

【讨论】:

  • 我会选择@Fals 的回答,然后再问问题,直到用户答对为止。
  • 当我以这种方式尝试时,它给了我一条消息,说这是非法的退货声明。它必须是一个函数才能完成我想要它做的事情吗?或者有没有办法用常规的 if 语句来做到这一点?
  • Rapjr11 让我想到将它放在一个 else 子句中,这样我就不必使用函数了。它工作得很好。感谢您的帮助。
【解决方案4】:

放入函数中,出错时返回。

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

var game = function() {
    var userChoice = prompt("Do you choose rock, paper or scissors?");

    if  (userChoice !== "rock" || "scissors" || "paper") {
        console.log("That is not one of the options");
        return;
    }

    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    console.log("Computer: " + computerChoice);
    compare(userChoice, computerChoice);
}

game();

【讨论】:

  • 你能举个简单的例子来说明你指的是什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2011-06-10
  • 1970-01-01
相关资源
最近更新 更多