C# 开关语法错误

C# switch syntax error

我有这个 switch 语句来测试我的 Grounded 整数变量的大小写,但我的 Unity-Monodelop 说我的代码中有我找不到的奇怪语法错误。我希望有人能告诉我它有什么问题。

private void JumpController () {
    if (Input.GetAxis("Jump")) { // if jump switch to action
        switch (Grounded) {
        0:  // On ground;
            Jump ();
            Grounded = 1;
            break;
        1:  // Jumped once;
            Jump ();
            Grounded = 2;
            break;
        2:  // Jumped twice;
            Debug.print ("Grounded = 2");
            break;
        default: break; 
        }
    }
}

An Image showing the errors

我建议在您的案例前添加一个 case。这应该修复错误:

private void JumpController () {
    if (Input.GetAxis("Jump")) { // if jump switch to action
        switch (Grounded) {
        case 0:  // On ground;
            Jump ();
            Grounded = 1;
            break;
        case 1:  // Jumped once;
            Jump ();
            Grounded = 2;
            break;
        case 2:  // Jumped twice;
            Debug.print ("Grounded = 2");
            break;
        default: break; 
        }
    }
}

您需要为每个案例定义案例关键字。例如:

switch (Grounded) {
    case 0 :
    // something
    break;

}

https://msdn.microsoft.com/en-gb/library/06tc147t.aspx