【问题标题】:Unable to evaluate String class in switch statement in JRE7无法在 JRE7 的 switch 语句中评估 String 类
【发布时间】:2013-05-28 10:38:04
【问题描述】:

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

第 5 行的错误消息:无法打开 String 类型的值。只允许可转换的 int 值或枚举常量

Eclipse 是否指向 JRE7? = 是的。 环境变量是否指向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;
   }

    }
}

【问题讨论】:

  • 你的主类的名字是SwitchDemo,而不是switchDemo
  • 当然。它现在有效,但仅在我使用 cmd 编译时才有效。但是 Eclipse 一直显示编译错误。也许我应该重新启动我的电脑

标签: java string switch-statement


【解决方案1】:

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

来自您的 cmets:

Eclipse 不断返回错误消息。我目前在 Eclipse 赫利俄斯。我会切换到朱诺。谢谢

你必须在eclipse中把你的编译器改成1.7:

【讨论】:

  • Eclipse 不断返回错误消息。我目前正在使用 Eclipse Helios。我会切换到朱诺。谢谢
  • @Rahoul 不需要。我想你需要将你的编译器更新到 jdk 1.7。
  • 我的列表中没有 1.7。最高只有 1.6。请看新的截图。
  • 您必须使用更新版本的 Eclipse。 stackoverflow.com/questions/8022233/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2022-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多