【问题标题】:Using switch() with Scanner in Java error when running with Pico Compiler- Ipad使用 Pico Compiler-Ipad 运行时在 Java 中使用带有 Scanner 的 switch() 错误
【发布时间】:2017-04-21 12:45:52
【问题描述】:

我在 Eclipse 上运行以下代码,没有任何错误。当我尝试在我的 Ipad 上对 Pico 编译器应用程序执行相同操作时,我得到 4 种类型的错误。附上打印屏幕。知道为什么不能以相同的方式运行吗?

代码:

"'''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''

import java.util.Scanner;

public class switchClass {

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a command: ");
    String text = scanner.nextLine();
    scanner.close();
    switch(text) {
    case "start":
        System.out.println("Machine started");
        break;

    case "stop":
        System.out.println("Machine stopped");
        break;  

    default:
        System.out.println("Command not recognized!");
    }

    }

}

'''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''

Pico Compiler for Ipad 运行代码:

Pico 编译器错误:

【问题讨论】:

  • 据我所知,切换Strings 是在 Java 7 中引入的。也许您的 Pico 编译器版本不(完全)支持 Java 7?

标签: java eclipse ipad


【解决方案1】:

错误提示您不能在case 中使用String

switch 中使用String 是在Java-7 中引入的,因此我怀疑Pico Compiler 使用Java-6 或下面。

修复Use string in switch case in java

【讨论】:

  • 感谢您的回答。很可能就是这个原因。由于我目前正在学习 Java,而且大多数时候随身携带平板电脑而不是笔记本电脑很容易,您能推荐一个可以支持当前 Java 版本的在线编译器吗?
猜你喜欢
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
相关资源
最近更新 更多