小编Anu*_*rag的帖子

如何在Java的Switch的Case值中添加字符串数组项?

我们知道,从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)

java arrays string switch-statement

0
推荐指数
1
解决办法
79
查看次数

标签 统计

arrays ×1

java ×1

string ×1

switch-statement ×1