java Switch 语句给出不兼容的类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12521355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch Statement gives Incompatible Types error
提问by user1514362
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;
}
回答by kosa
switch(name)
switch
statement with String is supported from Java7 onwards only.
switch
仅从 Java7 开始支持带有 String 的语句。
I guess the compiler version you are using is less than Java7
我猜你使用的编译器版本低于Java7
Options:
选项:
1) You need to either upgrade to Java7
2) Change switch statement to `if/else`
3) Use `int` in switch instead of `String`
回答by PermGenError
switchaccepts a String from java 7. prior to java 7only int compatible types (short,byte,int, char)can be passed as switch arguments
switch接受来自 java 7的String。在 java 7 之前,只有int 兼容类型(short、byte、int、char)可以作为开关参数传递
回答by Suraj Jogdand
If you are using maven project then simply you can add following piece of code to ypur pom.xml and resolve your problem :
如果您使用的是 maven 项目,那么您只需将以下代码添加到 ypur pom.xml 并解决您的问题:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
回答by Alex Coleman
You cannot switch over a String instance, only int (and byte/char/short, but not long/double), unless you have Java7+. Your best option now is to change to if else statements, like so:
您不能切换 String 实例,只能切换 int(和 byte/char/short,但不能切换 long/double),除非您有 Java7+。您现在最好的选择是更改为 if else 语句,如下所示:
if("B".equals(string)) {
//handle string being "B"
} else if("C".equals(string)) {
//handle string being "C"
} else ...
For more info on switching, see the Oracle tutorial. They mention the Java7 String functionality:
有关切换的更多信息,请参阅 Oracle教程。他们提到了 Java7 字符串功能:
In Java SE 7 and later, you can use a String object in the switch statement's expression.
在 Java SE 7 及更高版本中,您可以在 switch 语句的表达式中使用 String 对象。
回答by Don Branson
In Java, you can only do a switch on byte, char, short, or int, and not a String.
在 Java 中,您只能对 byte、char、short 或 int 进行切换,而不能对 String 进行切换。