我正在尝试编译,但出现此错误:

enigma/Rotor.java:30: incompatible types found : java.lang.String required: int     switch(name){
1 error

为什么会出现此错误?我如何解决它?它在包装中,我似乎无法弄清楚。这是代码:
String label;

Rotor(){;}

Rotor(String name){
  switch(name){
    case "B":
      conversion_chart = B;
      break;
    case "C":
      conversion_chart = C;
      break;
    case "I":
      conversion_chart=I;
      notch = NOTCH[0];
      break;
    case "II":
      conversion_chart=II;
      notch = NOTCH[1];
      break;
    case "III":
      conversion_chart=III;
      notch = NOTCH[2];
      break;
    case "IV":
      conversion_chart=IV;
      notch = NOTCH[3];
      break;
    case "V":
      conversion_chart=V;
      notch = NOTCH[4];
      break;
    case "VI":
      conversion_chart=VI;
      notch = NOTCH[5];
      break;
    case "VII":
      notch = NOTCH[6];
      conversion_chart=VII;
      break;
    case "VIII":
      notch = NOTCH[7];
      conversion_chart=VIII;
      break;
  }
  label = name;
  position = 0;
}

最佳答案

switch(name)
从Java7开始仅支持带String的switch语句。
我猜您正在使用的编译器版本低于Java7
选项:
  • 您需要升级到Java7
  • 将switch语句更改为if/else
  • 在开关中使用int代替String
  • 08-08 02:00