【问题标题】:Compare a number in a switch statement比较 switch 语句中的数字
【发布时间】:2026-01-04 05:00:01
【问题描述】:

我需要比较 switch 语句中的 int,但我不确定我是如何输入错误的。这是我的代码:

switch (y) {
    case int y isgreater(1, 411):
       // case code here...

为了简化我想要的,在 VB 中,代码是:

Case >= 411:
  'Code here for case

【问题讨论】:

    标签: objective-c ios xcode switch-statement


    【解决方案1】:

    Objective-C 等基于 C 的语言不支持这种语法。只需使用if 声明:

    if (y >= 411) {
        // do stuff
    }
    

    switch 语句中,每个case 值必须是离散常量。

    switch (expression) {
        case 5:
            // stuff
            break;
        case 12:
            // stuff
            break:
        default:
            // stuff
            break;
    }
    

    【讨论】: