在Javascript switch-statement中运行复杂的操作

时间:2014-01-28 00:48:13

标签: javascript switch-statement

所以我正在编写一个基本程序来与用户一起玩石头剪刀,我的一个开关没有合作。代码如下:

console.log("start rokpaperscissors")
var userString = prompt("Do you choose rock, paper, or scissors")
var computerRandom = Math.random()
//convert computerRandom into value and string
switch(computerRandom) {
    case computerRandom < 0.33: computerString = "rock"; computerValue = 2
        break;
    default: computerString = "paper"; computerValue = 4
        break;
    case computerRandom > 0.66: computerString = "scissors"; computerValue = 6
        break;
}
//convert userString into value
switch(userString) {
    case "rock": userValue = 2
        break;
    case "paper": userValue = 4
        break;
    case "scissors": userValue = 6
        break;
    default: console.log("debug @ line 12")
}
switch(userValue) {
    //if user wins, this code should run
    case userValue > computerValue || computerValue === 6 && userValue === 2:
        document.write("computer's choice: ")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("you win")
        break;
    //if user loses, this code should run
    case userValue < computerValue || computerValue === 2 && userValue === 6: 
        document.write("computer's choice:")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("sorry, you lose")
        break;
    //if userValue === computerValue, they tie and this code runs
    case computerValue:
        document.write("computer's choice: ")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("tie game")
        break;
    default: console.log("debug @ line 22")
}

在最终开关上,用于显示输出,代码始终跳转到默认代码并打印debug @ line 22消息。我怀疑这是由于“案例”行中的复杂操作。这些操作在交换机中是不可能的,如果是这样,我的其他选择是什么?如果他们被允许,他们的格式错误吗?

感谢您的帮助

- 连帽狮鹫

-------------------编辑--------------------------- -------------------------------------

我通过组合一个“if-else if”语句来定义win,lose和tie,用switch语句输出结果来解决这个问题:这是我从23行开始的最终代码(前22行是相同)

//define win, lose, and tie cases as such
if(userValue === computerValue) {var resultString = "tie"}
    else if(userValue > computerValue || computerValue === 6 && userValue === 2) {var resultString = "win"}
    else if(userValue < computerValue || computerValue === 2 && userValue === 6) {var resultString = "lose"}
    else {console.log("debug @ line 56")}
//output result
switch(resultString) {
    case "win":
        document.write("computer's choice: ")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("you win")
        break;
    case "lose":
        document.write("computer's choice: ")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("sorry, you lose")
        break;
    case "tie":
        document.write("computer's choice: ")
            document.write(computerString)
            document.write("<br/>")
        document.write("your choice: ")
            document.write(userString)
            document.write("<br/>")
        document.write("tie game")
        break;
}

感谢jdigital,Matthew Booth和Matthew Lock帮助解决这个问题。如果您对此有任何疑问,我认为您仍然可以发表评论,我会尽力回复。

谢谢 - 连帽狮鹫

3 个答案:

答案 0 :(得分:1)

您基本上可以将 switch 值换为 true,这样您从条件中获得的结果与该值匹配,并且可以应用特定情况,例如:

switch(true){
 case userValue < computerValue || computerValue === 2 && userValue === 6: 
  console.log("case x is true");
  break;
}

==> 如果这种情况属实,console.log 将适用

答案 1 :(得分:0)

编辑:userValue是int类型。您的开关中的个案是布尔型。重构switch语句,以便case使用与switch参数相同的var类型。

修改:与初始问题无关,但仍然有用

尝试将比较运算符分解为新案例。 以下是参考:multiple cases

以下是该帖子的代码供参考:

switch (varName)
{
    case "afshin":
    case "saeed":
    case "larry": 
       alert('Hey');
    break;

    default: 
        alert('Default case');
    break;
}

此外,这是一个猜测,是否在括号帮助中包含多个比较?

(userValue > computerValue || computerValue === 6 && userValue === 2)

答案 2 :(得分:0)

switch语句不能使用范围。 switch case标签必须是整数的常量(也包括&#34; char&#34;或字符串)。您的案例标签正在考虑switch语句未设计的内容,浮点值以及两个不同变量的比较。案例标签必须是常量。