有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中使用SWITCH CASE语句中的函数/方法

我有三种类型的数组: ControlType ControlName ControlValue

当控件类型为“ClickButton”时,我必须传递相应的ControlName和ControlValue

代码:

public class StringSwitchDemo extends StringSwitchDemoHelper
{
    public static int getMonthNumber(String ControlType) {
        int ControlValue = 0;
        int ControlName  = 0;

        if (ControlType == null) {
            return ControlValue;
        }

        switch (ControlType.toString()) {
            case ClickButton:
                return ControlType(ControlValue, ControlName);
                break;
        }

        return ControlValue;
    }

    private static int ControlType(String controlValue, String controlName) {
        // TODO Auto-generated method stub

        return 0;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在switch语句中只能使用常量(最终变量)和文本

    public static final String MY_CONST = "foo";
    
    public static int getMonthNumber(String ControlType) {
        int ControlValue = 0;
        int ControlName  = 0;
    
        if (ControlType == null) {
            return ControlValue;
        }
    
        switch (ControlType) {
            case MY_CONST: // this will work
                break;
            case "ClickButton":
                return ControlType(ControlValue, ControlName);
        }
    
        return ControlValue;
    }
    
    private static int ControlType(int controlValue, int controlName) {
        // TODO Auto-generated method stub
    
        return 0;
    }
    

    另外,请阅读Java的naming conventions