Switch Statement gives Incompatible Types error(Switch 语句给出不兼容类型错误)
问题描述
我正在尝试编译并收到此错误:
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)
switch
语句仅从 Java7 开始支持.
switch
statement with String is supported from Java7 onwards only.
我猜你使用的编译器版本低于Java7
I guess the compiler version you are using is less than Java7
选项:
- 您需要升级到 Java7
- 将 switch 语句更改为
if/else
- 在 switch 中使用
int
而不是String
这篇关于Switch 语句给出不兼容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Switch 语句给出不兼容类型错误


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01