出于某种原因,String[]冠军没有被实例化,我似乎不明白为什么
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;
}发布于 2012-11-28 17:55:32
你是对的,如果调用其中一个case语句,它将初始化数组。我会在其中添加一个default,然后在调用该default时抛出。当没有匹配的case语句时,将调用默认值。看起来您已经覆盖了所有的case语句,所以使用default抛出将是一个好主意。
像这样的东西应该就行了
default: throw new RuntimeException("SHould not be here " + rolereturn);发布于 2012-11-28 18:22:36
我会用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-28 17:56:11
最可能的原因是rolereturns不是您想的那样的问题。您是否可以尝试在开关的末尾添加一个默认值,将数组设置为某个值,并查看是否可以这样做?
否则,只需调试并检查您是否进入了正确的行...
https://stackoverflow.com/questions/13602275
复制相似问题