我如何简化这个过于冗长的Swift switch语句?

huangapple go评论58阅读模式
英文:

How do I simplify this overly long Swift switch statement?

问题

let strStatResult = strStat
strengthModLabel.text = String(strStatResult)

switch strStatResult {
case 1:
    strengthModLabel.text = "-5"
case 2, 3:
    strengthModLabel.text = "-4"
case 4, 5:
    strengthModLabel.text = "-3"
case 6, 7:
    strengthModLabel.text = "-2"
case 8, 9:
    strengthModLabel.text = "-1"
case 10, 11:
    strengthModLabel.text = "0"
case 12, 13:
    strengthModLabel.text = "+1"
case 14, 15:
    strengthModLabel.text = "+2"
case 16, 17:
    strengthModLabel.text = "+3"
case 18, 19:
    strengthModLabel.text = "+4"
case 20, 21:
    strengthModLabel.text = "+5"
case 22, 23:
    strengthModLabel.text = "+6"
case 24, 25:
    strengthModLabel.text = "+7"
case 26, 27:
    strengthModLabel.text = "+8"
case 28, 29:
    strengthModLabel.text = "+9"
case 30:
    strengthModLabel.text = "+10"
default:
    strengthModLabel.text = "Error"
}
英文:

I have six of these code blocks and am wanting to shorten all of them while still having them work effectively. This code works the way that I intended it to but is really bulky.

    let strStatResult = strStat
    strengthModLabel.text = String(strStatResult)
    
    switch strStatResult {
    case let strStatResult where strStatResult == 1:
        strengthModLabel.text = "-5"
    case let strStatResult where strStatResult == 2 || strStatResult == 3:
        strengthModLabel.text = "-4"
    case let strStatResult where strStatResult == 4 || strStatResult == 5:
        strengthModLabel.text = "-3"
    case let strStatResult where strStatResult == 6 || strStatResult == 7:
        strengthModLabel.text = "-2"
    case let strStatResult where strStatResult == 8 || strStatResult == 9:
        strengthModLabel.text = "-1"
    case let strStatResult where strStatResult == 10 || strStatResult == 11:
        strengthModLabel.text = "0"
    case let strStatResult where strStatResult == 12 || strStatResult == 13:
        strengthModLabel.text = "+1"
    case let strStatResult where strStatResult == 14 || strStatResult == 15:
        strengthModLabel.text = "+2"
    case let strStatResult where strStatResult == 16 || strStatResult == 17:
        strengthModLabel.text = "+3"
    case let strStatResult where strStatResult == 18 || strStatResult == 19:
        strengthModLabel.text = "+4"
    case let strStatResult where strStatResult == 20 || strStatResult == 21:
        strengthModLabel.text = "+5"
    case let strStatResult where strStatResult == 22 || strStatResult == 23:
        strengthModLabel.text = "+6"
    case let strStatResult where strStatResult == 24 || strStatResult == 25:
        strengthModLabel.text = "+7"
    case let strStatResult where strStatResult == 26 || strStatResult == 27:
        strengthModLabel.text = "+8"
    case let strStatResult where strStatResult == 28 || strStatResult == 29:
        strengthModLabel.text = "+9"
    case let strStatResult where strStatResult == 30:
        strengthModLabel.text = "+10"
    default:
        strengthModLabel.text = "Error"
    
    }

I've thought of using for loops or while loops to shorten it but I am not sure how to implement that all the while shortening the code.

答案1

得分: 1

这个结果似乎是 strStatResult / 2 - 5。这适用于整数除法。 10 / 2 和 11 / 2 都是 5。 减去 5 就得到 0,就像你的巨大开关语句一样。

假设 strStatResult 是一个 Int(或其他整数类型),你只需要:

if strStatResult < 1 || strStatResult > 30 {
    strengthModLabel.text = "错误"
} else {
    let num = strStatResult / 2 - 5
    strengthModLabel.text = num > 0 ? "+\(num)" : "\(num)"
}
英文:

The result seems to be strStatResult / 2 - 5. This works with integer division. Both 10 / 2 and 11 / 2 give 5. Subtract 5 and you get 0, just like your giant switch statement.

Assuming strStatResult is an Int (or other integer type), all you need is:

if strStatResult < 1 || strStatResult > 30 {
    strengthModLabel.text = "Error"
} else {
    let num = strStatResult / 2 - 5
    strengthModLabel.text = num > 0 ? "+\(num)" : "\(num)"
}

huangapple
  • 本文由 发表于 2023年4月7日 04:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定