gpt4 book ai didi

java - 使用 Pico 编译器 - Ipad 运行时,在 Java 中使用 switch() 和 Scanner 会出现错误

转载 作者:行者123 更新时间:2023-12-02 13:21:48 25 4
gpt4 key购买 nike

我在 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!");
}

}

}

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

适用于 Ipad 的 Pico 编译器运行代码:

enter image description here

Pico 编译器错误:

enter image description here

最佳答案

错误表明您不能在 case 中使用String

switch中使用String是在Java-7中引入的,因此,我怀疑Pico编译器使用< strong>Java-6 或更低版本。

修复:Use string in switch case in java

关于java - 使用 Pico 编译器 - Ipad 运行时,在 Java 中使用 switch() 和 Scanner 会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com