Switch Statement给出了不兼容类型错误 [英] Switch Statement gives Incompatible Types error

查看:343
本文介绍了Switch Statement给出了不兼容类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译并收到此错误:

I am trying to compile and I get this error:

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

为什么我收到这个错误?我如何解决它?它在包中,我似乎无法弄明白。这是代码:

Why am I getting this error? How do I fix it? It's in the package and I can't seem to figure it out. Here's the code:

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 statement with String is supported from Java7 onwards only.

我猜你使用的编译器版本小于Java7

I guess the compiler version you are using is less than Java7

选项:

1) You need to either upgrade to Java7 
2) Change switch statement to `if/else`
3) Use `int` in switch instead of `String`

这篇关于Switch Statement给出了不兼容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆