2017-03-28 83 views
-2

我有3種類型的數組的: ControlType ControlName ControlValue如何使用功能/ SWITCH CASE語句方法在Java中

當控制類型是「ClickButton」,我必須通過相應的控件名稱和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; 
    } 
} 
+0

你不需要(也不能在'return'後面加上'break',因爲它是無法訪問的。 「break」永遠不會被打中,因爲方法已經返回。 – khelwood

+0

'ClickButton'是編譯時常量字符串嗎? – khelwood

+0

@khelwood新用戶似乎只問問題而不回答!!!! –

回答

0

您只能使用switch語句中的常量(final的變量)和文字。

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

0
I simplified like below: 

`import resources.StringSwitchDemoHelper; 
/** 
* Description : Functional Test Script 
* @author IBM 
*/ 


public class StringSwitchDemo extends StringSwitchDemoHelper 
{ 

    public static String getControlValue(String controlType) { 

     String controlValue = null; 
     String controlName = null; 

     if (controlType == null) { 
      return controlValue; 
     } 

     switch (controlType) { 
      case "Html.INPUT.text": 
       return EnterText (controlName,controlValue); 
     } 
    return controlValue; 

    } 

public static String EnterText(String controlName, String value) 
{ 
    TextGuiTestObject textObj = findList(controlName); 
    if (textObj != null) { 
    textObj.setText(value); 
    } else { 
    throw new ObjectNotFoundException(); 
    } 
    return value;} 
}` 

Still it is not working!!