public static final int EMPLOYEE = 10;
public static final int PROGRAMMER = 11;
public static final int DESIGNER = 12;
public static final int ARCHITECT = 13;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
//Employee : 10, id, name, age, salary
//Programmer: 11, id, name, age, salary
//Designer : 12, id, name, age, salary, bonus
//Architect : 13, id, name, age, salary, bonus, stock
public static final String[][] EMPLOYEES = {
{"10", "1", "马云", "22", "3000"},
{"13", "2", "马化腾", "32", "18000", "15000", "2000"},
{"11", "3", "李彦宏", "23", "7000"},
{"11", "4", "刘强东", "24", "7300"},
{"12", "5", "雷军", "28", "10000", "5000"},
{"11", "6", "任志强", "22", "6800"},
{"12", "7", "柳传志", "29", "10800","5200"},
{"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
{"12", "9", "史玉柱", "26", "9800", "5500"},
{"11", "10", "丁磊", "21", "6600"},
{"11", "11", "张朝阳", "25", "7100"},
{"12", "12", "杨致远", "27", "9600", "4800"}
};
---------------------------------------------------------------
int type = Integer.parseInt(EMPLOYEES[i][0]);
switch (type){
case EMPLOYEE:
break;
case PROGRAMMER:
break;
case DESIGNER:
break;
case ARCHITECT:
break;
}
type的值是10,应该是case 10:,为什么可以是case EMPLOYEE?
该回答引用GPTᴼᴾᴱᴺᴬᴵ
在这个代码片段中,常量 EMPLOYEE 的值是 10,因此 case EMPLOYEE 和 case 10 在语义上是等价的。在 Java 中,case 语句可以使用整数常量,枚举常量或 final 常量等,因此可以使用 EMPLOYEE 常量来代替数值常量 10。这种方式可以增加程序的可读性和可维护性。所以在 switch-case 结构中,可以直接使用常量 EMPLOYEE。
employee的值也是10啊,这样写无非就是为了可读性更好,知道10代表的是employee。