英文:
Java: how to use a variable of Enum type in the main function?
问题
public class GamesLevels{
enum Level{EASY,MODERATE,HARD}
public String whatLevel(Level level){
String myAns = "";
if (level == Level.EASY) {
myAns = "欢迎来到游戏";
}
else if (level == Level.MODERATE) {
myAns = "哇,你升级了!";
}
else if (level == Level.HARD) {
myAns = "厉害了,我们有一个专家!";
}
return myAns;
}
public static void main(String[] args) {
GamesLevels game = new GamesLevels();
System.out.println(game.whatLevel(Level.HARD));
}
}
英文:
I am trying to print a specific statement using variable type Level
(an enum) as my argument. When I run the print statement System.out.println(whatLevel(HARD))
I get an error. How can I make this work?
public class GamesLevels{
enum Level{EASY,MODERATE,HARD}
public String whatLevel(Level level){
Sring myAns = "";
if (level == Level.EASY) {
myAns = "welcome to the game";
}
else if (level == Level.MODERATE) {
myAns = "Wow, you leveled up!";
}
else if (level == Level.HARD) {
myAns = "Nice, we have a Pro!";
}
return myAns;
}
public static void main(String[] args) {
System.out.println(whatLevel(HARD));
}
}
答案1
得分: 0
枚举在编译时处理,每个条目都有预定义的状态,因此您应该像这样使用它:
YOUR_ENUM.VALUE1
另外,main
是一个静态方法,只能直接调用其他静态方法(无需对象)
请按以下方式修改您的代码:
class GamesLevels {
enum Level {
EASY, MODERATE, HARD
}
public static String whatLevel(Level level){
String myAns = "";
if (level == Level.EASY) {
myAns = "欢迎进入游戏";
}
else if (level == Level.MODERATE) {
myAns = "哇,您升级了!";
}
else if (level == Level.HARD) {
myAns = "不错,我们有个专家!";
}
return myAns;
}
public static void main(String[] args) {
System.out.println(whatLevel(Level.HARD));
}
}
输出:
不错,我们有个专家!
英文:
Enums are processed at compile time, they have predefined state for each of its entry, hence you should be using it like
YOUR_ENUM.VALUE1
Also, main
being a static method can call only other static methods directly (without object)
Modify your code like below,
class GamesLevels {
enum Level {
EASY, MODERATE, HARD
}
public static String whatLevel(Level level){
String myAns = "";
if (level == Level.EASY) {
myAns = "welcome to the game";
}
else if (level == Level.MODERATE) {
myAns = "Wow, you leveled up!";
}
else if (level == Level.HARD) {
myAns = "Nice, we have a Pro!";
}
return myAns;
}
public static void main(String[] args) {
System.out.println(whatLevel(Level.HARD));
}
}
Output:
Nice, we have a Pro!
答案2
得分: 0
这里实际上涉及到几件事情。
首先,在你在 main
函数内调用 whatLevel()
函数时,你传递了 HARD
作为参数。这可能不会起作用,因为 HARD
实际上是枚举类型 Level 的一个选项。类似于在函数定义的 if 语句中的表述方式,这应该被替换为 Level.HARD
。
其次,你正试图在 main
函数内调用一个非静态方法。每个程序只有一个主函数,所以在其中调用的任何函数本质上都是静态的。在 whatLevel()
函数定义中添加 static 修饰符应该可以解决这个问题,但我建议你如果还不知道的话,可以研究一下这个修饰符实际上是做什么用的。
经过这些变化后的代码如下所示,可以正常工作:
public class GamesLevels {
public enum Level {
EASY,
MODERATE,
HARD
}
public static String whatLevel(Level level){
String myAns = "";
if (level == Level.EASY) {
myAns = "欢迎来到游戏";
}
else if (level == Level.MODERATE) {
myAns = "哇,你升级了!";
}
else if (level == Level.HARD) {
myAns = "不错,我们有位专家!";
}
return myAns;
}
public static void main(String[] args ) {
System.out.println(whatLevel(Level.HARD));
}
}
英文:
There are actually a couple things going on here.
First, when you call your whatLevel()
function inside main, you pass HARD
as an argument. This will likely not work, as HARD is actually on option for the value of the enumeration called Level. Similar to how it is framed inside the if statements of your function definition, this should actually be replaced with Level.HARD
.
Second, you are attempting to call a non-static method within your main function. Each program only has one main function, so any functions called inside of it are inherently static. Adding the static modifier to your whatLevel()
function definition should fix this, but I would recommend researching what the modifier actually does, if you do not know already.
The code with these changes implemented looks like this, and works as expected:
public class GamesLevels {
public enum Level {
EASY,
MODERATE,
HARD
}
public static String whatLevel(Level level){
String myAns = "";
if (level == Level.EASY) {
myAns = "welcome to the game";
}
else if (level == Level.MODERATE) {
myAns = "Wow, you leveled up!";
}
else if (level == Level.HARD) {
myAns = "Nice, we have a Pro!";
}
return myAns;
}
public static void main(String[] args ) {
System.out.println(whatLevel(Level.HARD));
}
}
答案3
得分: 0
你还可以将每个枚举的描述合并到声明中:
public class GamesLevels {
enum Level {
EASY("欢迎来到游戏"),
MODERATE("哇,你升级了!"),
HARD("不错,我们有高手!");
private String desc;
Level(String desc) {
this.desc = desc;
}
String getDescription() {
return desc;
}
}
public static void main(String[] args ) {
System.out.println(Level.HARD.getDescription());
}
}
这样做的好处是,如果以后添加一个新的级别,如果没有添加描述,代码将无法编译。而使用 whatLevel
的话,不管是否添加描述,都会正常运行但不打印任何内容。
或者使用更简洁的版本:
public class GamesLevels {
enum Level { EASY, MODERATE, HARD };
private static Map<Level, String> descriptions = Map.of(
Level.EASY, "欢迎来到游戏",
Level.MODERATE, "哇,你升级了!",
Level.HARD, "不错,我们有高手!");
public static void main(String[] args ) {
System.out.println(descriptions.getOrDefault(Level.HARD, "未知"));
}
}
英文:
You could also incorporate the description of each enum into the declaration:
public class GamesLevels {
enum Level {
EASY("welcome to the game"),
MODERATE("Wow, you leveled up!"),
HARD("Nice, we have a Pro!");
private String desc;
Level(String desc) {
this.desc = desc;
}
String getDescription() {
return desc;
}
}
public static void main(String[] args ) {
System.out.println(Level.HARD.getDescription());
}
}
This benefit that if you later add a level it won't compile without adding the description, whereas whatLevel would work as is and print nothing.
Or a shorter version:
public class GamesLevels {
enum Level { EASY, MODERATE, HARD };
private static Map<Level,String> descriptions = Map.of(
Level.EASY, "welcome to the game",
Level.MODERATE, "Wow, you leveled up!",
Level.HARD, "Nice, we have a Pro!");
public static void main(String[] args ) {
System.out.println(descriptions.getOrDefault(Level.HARD, "UNKNOWN"));
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论