-
运行环境:
JDK8 + IntelliJ IDEA 2018.3
-
源码
Demo类:
package com.ChenJiangT.demo2;
import java.util.Arrays;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
System.out.println(Arrays.toString(Season.values()));
Season[] sea=Season.values();
for (Season a:sea) {
System.out.println(a);
System.out.println(a.name());
System.out.println(a.ordinal());
}
Scanner sc=new Scanner(System.in);
System.out.println("请输入您想要的季节(1--SPRING,2---SUMMER,3--AUTUMN,4--WINTER)");
String s=sc.next();
Season season = Season.valueOf(s);
fun(season);
}
public static void fun(Season season){
switch (season){
case SUMMER:
System.out.println("夏天来了");
break;
case AUTUMN:
System.out.println("秋天来了");
break;
case WINTER:
System.out.println("冬天来了");
break;
default:
System.out.println("您的输入有误!");
}
}
}
Season 类:
package com.ChenJiangT.demo2; public enum Season { SPRING,//春 SUMMER,//夏 AUTUMN,//秋 WINTER;//冬 }
-
效果图: