快速比较 switch 语句中的元组与 or

标签 swift switch-statement tuples

如何在 switch 语句中为一个元组设置多种可能性?注意我试过了

var duel = (comp1CurrCard, comp2CurrCard)
    switch duel {

        case (1||14||27||40, 1||14||27||40):
            println("ace duel")

        case (2,15,28,41),(2,15,28,41):
            println("2 duel")
    }

comp1CurrCardcomp2CurrCard 都是 Int 类型。 基本上我想要的是 if comp1CurrCard == 1 || 14 || 27 || 40 && comp2CurrCard == 1 || 14 || 27 || 40比println("ace duel")

但是我不知道如何最好地做到这一点,我知道我希望使用 switch 语句,因为它似乎是解决它的最佳方法

由于错误,我知道我做错了什么 :P 感谢任何帮助!

最佳答案

我能想到这个解决方案,

var duel = (2, 2)

switch duel{
case let (m, n) where (m == 1 || m == 14 ||  m == 27 || m == 40) && (n == 2 || n == 14 || n == 27 || n == 40):
  println("ace duel")
case let (m, n) where (m == 2 || m == 15 || m == 28 || m==41) && (n == 2 || n == 15 || n == 28 || n == 41):
  println("2 duel")
default:
  println("No")
}

关于快速比较 switch 语句中的元组与 or,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008044/

相关文章:

iphone - objective-c 开关语句中的OR运算符

methods - 对可变结构参数的元组赋值

python - 仅将非空列表转换为列表列表中的元组

ios - 这里有什么问题 : Instance member cannot be used on type

ios - 无论如何保存推送通知?

ios - 我们可以在 native iOS 应用程序中创建 localhost

c++ - "structural binding"上的提案在哪里?

arrays - 如何检查 ANY 的数组包含哪种数据类型的元素

c++ - switch 优于 if-else 语句的优点

java - 在 Java switch 语句中使用访问器方法