1

由于某种原因,String[] champs 没有被实例化,我似乎看不出为什么
不应该 champs = new String[]{ "BLAH", "BLAH", "BLAH"};初始化数组?

  public static String[] getChamps() {

    String rolereturn = ChampSelect.getRoles();

    //Switch to Determine Champion Suggestions


    String[] champs; //String Declaration

    switch (rolereturn) {

        case "AD Carry": //AD Carry Selection Options
            champs = new String[]{ "Ashe", "Caityln", "Draven", "Ezreal", "Kog'Maw", "Sivir", "Twitch", "Varus", "Vayne" };
            break;

        case "AP Carry": //AP Carry Selection Options
            champs = new String[]{ "Diana", "Evelyn", "Kassadin", "Kennen" };
            break;

        case "Support": //Support Selection Options
            champs = new String[]{ "Janna", "Nunu", "Shen", "Soraka", "Taric", };
            break;

        case "AP Jungle": //AP Jungle Selection Options
            champs = new String[]{ "Diana", "Fiddlesticks" };
            break;

        case "AD Jungle": //AD Jungle Selection Options
            champs = new String[]{ "Kha'Zix", "Nocturne", "Rengar", "Udyr", "Warwick", };
            break;

        case "AP Top": //AP Top Selection Options
            champs = new String[]{ "Akali", "Cho'Gath", "Kennen", "Malphite", "Shen", "Singed", "Teemo" };
            break;

        case "AD Top": //AD Top Selection Options
            champs = new String[]{ "Fiora", "Irelia", "Jax", "Kha'Zix", "Master Yi", "Nasus", "Nidalee", "Rengar", "Zed" };
            break;
    }
    return champs;
}
4

3 回答 3

4

你是对的,如果调用其中一个 case 语句,它将初始化数组。我会在其中添加一个default,然后在default调用时抛出。当没有匹配 case 语句时调用默认值。看起来您正在涵盖所有案例陈述,因此default抛出将是一个好主意。

像这样的事情应该这样做

 default: throw new RuntimeException("SHould not be here " + rolereturn);
于 2012-11-28T09:55:32.360 回答
1

我会使用一个Map而不是一个开关盒,

public static String[] getChamps() {

    Map<String,String[]> map = new HashMap();
    map.put("AD Carry",new String[]{ "Ashe", "Caityln", "Draven", "Ezreal", "Kog'Maw", "Sivir", "Twitch", "Varus", "Vayne" });
    // ... and so on for all your cases

    String rolereturn = ChampSelect.getRoles();
    if (!map.containsKey(rolereturn)) throw new IllegalArgumentException(rolereturn);

    return map.get()    
}
于 2012-11-28T10:22:35.593 回答
0

最可能的原因是与rolereturns您的想法不同。您能否尝试在开关末尾添加一个默认值,将数组设置为某个值,看看是否可以

否则,只需调试并检查您是否进入正确的行...

于 2012-11-28T09:56:10.903 回答