定义了难度等级1,2,3,怎么通过enum转化成“难“易“中等”””
时间: 2023-10-03 21:03:11 浏览: 32
可以使用枚举类型的构造函数和属性来将难度等级转化为“难”、“易”和“中等”。下面是一个示例代码:
```
enum Difficulty {
EASY(1, "易"),
MEDIUM(2, "中等"),
HARD(3, "难");
private int level;
private String name;
private Difficulty(int level, String name) {
this.level = level;
this.name = name;
}
public String getName() {
return name;
}
}
// 使用示例
Difficulty difficulty = Difficulty.MEDIUM;
System.out.println("难度等级:" + difficulty.ordinal());
System.out.println("难度名称:" + difficulty.getName());
```
在上面的代码中,我们首先定义了一个枚举类型Difficulty,其中包含三个常量EASY、MEDIUM和HARD,它们分别对应难度等级1、2和3。在枚举类型中,我们定义了一个构造函数和level、name两个属性,用来将难度等级转化为“难”、“易”和“中等”。getName()方法用于获取难度等级对应的名称。
在使用时,我们可以通过Difficulty枚举类型来获取难度等级及其对应的名称。例如,difficulty.ordinal()方法获取难度等级的值,difficulty.getName()方法获取难度等级对应的名称。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)