javascript - 在 Javascript switch 语句中运行复杂的操作

标签 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”语句来定义赢、输和平局,并使用 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 在此问题上提供的帮助。如果您对此有任何疑问,我想您仍然可以评论,我会尽力回复。

谢谢——兜帽狮鹫

最佳答案

编辑:userValue 是 int 类型。开关中的情况是 bool 类型。重构该 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)

关于javascript - 在 Javascript switch 语句中运行复杂的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21394830/

相关文章:

javascript - 如何在 Shopify 上将产品颜色选项分为两个类别 "Essentials"和 "Limited Edition"?

swift - 是否可以使用 'if-case' 代替具有逗号分隔列表的开关?

Swift 4 switch 语句比较多个值,使用大于或小于

javascript - 网站实时聊天软件

javascript - Axios POST可在Node.js中工作,但不能在Electron中工作

javascript - Express - 错误处理中的函数原型(prototype) toString()?

Java - 非常长的 switch case 的替代方案

javascript - 仅当第一个字段有值时启用日期选择器

python - Python 中替代开关的性能差异

java - if/else 和 switch 语句的操作有区别吗?