作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为此工作了三天,但我无法解决任何问题。请帮忙!
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/
我是一名优秀的程序员,十分优秀!