无法在JRE7中的switch语句中计算String类

时间:2013-05-28 10:38:04

标签: java string switch-statement

我正在尝试构建一个简单的程序,演示如何在switch语句中使用正在评估的String类,但是我遇到了编译错误,因为此功能需要Java SE 7(我的计算机中安装了这个)。 / p>

第5行的错误消息:无法打开String类型的值。只允许使用可转换的int值或enum consants

Eclipse是否指向JRE7? =是。 Environnement变量指向JDK7 = YES。

java -version = OK(见下图)。 enter image description here

我甚至尝试使用控制台提示符编译代码。看错误: enter image description here

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;
   }

    }
}

enter image description here

1 个答案:

答案 0 :(得分:8)

从命令行我看到您运行java -cp .switchDemo,而您的实际班级名称是 SwitchDemo 。请注意,Java区分大小写,因此SwitchDemo != switchDemo

来自您的评论:

  

Eclipse不断返回错误消息。我目前正在使用Eclipse   太阳神。我会切换到Juno。谢谢

你必须在eclipse中将你的编译器改为1.7:

enter image description here