switch()中写的是byte short ,char和int,实际上是int的值,byte short ,char都可以类型转换成int,所以没问题,
其中还可以放枚举类型的,枚举类型的可以和switch结合使用- public class EnumTest
- {
- public static void main(String[] args)
- {
- int a = 4, b = 2;
- Operation ope = Operation.ADD;
- calculate(a, b, ope);
- }
- public static void calculate(int a, int b, Operation ope)
- {
- switch (ope)
- {
- case SUB:
- System.out.println(a - b);
- break;
- case ADD:
- System.out.println(a + b);
- break;
- case MUL:
- System.out.println(a * b);
- break;
- case DIV:
- System.out.println(a / b);
- break;
- default:
- System.out.println("feifa");
- }
- }
- }
- enum Operation
- {
- ADD, SUB, MUL, DIV
- }
复制代码 |