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

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;
        }
    }
}

09-27 10:43