用java创建一个用表示季节的枚举类型Season,用户从键盘输入1~4之间的整数(包含1和4),输入对应的季节。
时间: 2023-05-23 16:06:26 浏览: 72
A:
```java
import java.util.Scanner;
public class SeasonTest {
enum Season {
SPRING(1), SUMMER(2), AUTUMN(3), WINTER(4);
private int value;
Season(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入1~4之间的整数:");
int input = sc.nextInt();
sc.close();
switch(input) {
case 1:
System.out.println("对应的季节为:" + Season.SPRING);
break;
case 2:
System.out.println("对应的季节为:" + Season.SUMMER);
break;
case 3:
System.out.println("对应的季节为:" + Season.AUTUMN);
break;
case 4:
System.out.println("对应的季节为:" + Season.WINTER);
break;
default:
System.out.println("输入的整数不在区间[1, 4]内!");
break;
}
}
}
```
运行结果:
```
请输入1~4之间的整数:2
对应的季节为:SUMMER
```
阅读全文