我们知道,从Java 7开始,Switch的表达式可以是String。因此,我正在开发一个应用程序,其中,当用户选择类别时,将根据类别值为他/她分配相关部门。这是代码:
public class Selector {
///String array to save the departments
private final static String[] DEPTS = {
"A",
"B",
"C",
"D"
};
//String array for the categories
private final static String[] CATEGORY = {
"Wind",
"Air",
"Fire",
"Cloud",
"River",
"Tree",
"Abc",
"Def"
};
//return the department when user selects a particular category item from above
public static String setDepartment(String category) {
switch(category){
case "Wind":
return DEPTS[0];
case "Air":
return DEPTS[1];
case "Fire": case "Cloud": case "River":
return DEPTS[2]; …Run Code Online (Sandbox Code Playgroud)