geo*_*van -4 java string switch-statement
我正在尝试构建一个简单的程序,演示如何在switch语句中使用String类,但我收到编译错误,因为此功能需要Java SE 7(我的计算机中已经安装了).
第5行的错误消息:无法打开String类型的值.只允许使用可转换的int值或enum consants
Eclipse是否指向JRE7?=是.Environnement变量指向JDK7 = YES.
java -version = OK(见下图).

我甚至试图用控制台提示符编译代码.看错误:

import javax.swing.JOptionPane;
public class SwitchDemo {
public static void main(String[] args) {
String name = "georges";
switch (name.toLowerCase()) {
case "Jhon":
JOptionPane.showMessageDialog(null, "Good morning, Jhon!");
break;
case "georges":
JOptionPane.showMessageDialog(null, "How's it going, georges?");
break;
case "sergei":
JOptionPane.showMessageDialog(null, "sergei, my old sergei!");
break;
case "Steph":
JOptionPane.showMessageDialog(null, "Afternoon lennert, how's the Steph?");
break;
default:
JOptionPane.showMessageDialog(null, "Pleased to meet you, xxxxxx.");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)

从命令行我看到你运行java -cp .switchDemo,而你的实际类名是SwitchDemo.请注意,Java区分大小写SwitchDemo != switchDemo.
从你的评论:
Eclipse不断返回错误消息.我现在在Eclipse Helios上.我会切换到Juno.谢谢
你必须在eclipse中将编译器更改为1.7:
| 归档时间: |
|
| 查看次数: |
2487 次 |
| 最近记录: |