gpt4 book ai didi

java - 我收到错误 : class, 接口(interface)或预期的枚举,但我无法弄清楚

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

我已经为此工作了三天,但我无法解决任何问题。请帮忙!

public static void displayType (String ntype)
{
switch (ntype)

case "African":
System.out.print ("This Elephant is African")
break;

case "Indian":
System.out.print ("This Elephant is Indian")
break;

default :
System.out.print ("This type of Elephant is invalid")

return ntype;
} //End Switch


}

这些是我收到的错误:

Lab2Unit4Psuedocode.java:108: error: class, interface, or enum expected
public static void displayType (String ntype)
^

Lab2Unit4Psuedocode.java:116: error: class, interface, or enum expected
case "Indian":
^

Lab2Unit4Psuedocode.java:120: error: class, interface, or enum expected
default :
^

Lab2Unit4Psuedocode.java:124: error: class, interface, or enum expected
} //End Switch
^
4 errors

最佳答案

请查看下面代码中的注释。

public class Lab2Unit4Psuedocode {

public static void displayType (String ntype) {
switch (ntype) { //you need the open curly brace
case "African":
System.out.println("This Elephant is African"); //<-- you need to terminate with semi-colons in Java
break;

case "Indian":
System.out.println("This Elephant is Indian");
break;

default:
System.out.println("This type of Elephant is invalid");
//some people add an explicit break here
//don't return anything. By definition, void means you return nothing.
}
}
} //always line up your curly braces

另请注意,在 String 数据类型上使用 switch 仅出现在 Java 7 中。所以这不适用于旧版本的 Java。

Java 需要大量练习 - 坚持下去!

关于java - 我收到错误 : class, 接口(interface)或预期的枚举,但我无法弄清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552734/

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