int aa = 1; switch (aa){ case Fruit.apple.getValue(): System.out.println("apple"); break; case Fruit.banana.getValue(): System.out.println("banana"); break;; case Fruit.pare.getValue(): System.out.println("pare"); break; }
错误提示:constant expression required;
解决方案
10
//switch case中,case这里的判断条件只能是常量表达式。 case Fruit.apple.getValue(): System.out.println("apple"); break;
你可以这样写:
switch (aa){ case 1: System.out.println("apple"); break; case 2: System.out.println("banana"); break; }
10
enum Fruit { Apple, Orange } Fruit a = Fruit.Apple; switch (a) { case Apple: case Orange: break; }