public static void main(String[] args) {
int type = 8;
eatFood(type);
}
public static void eatFood(int type){
if(1 == type){
System.out.println("火锅");
} else if (2 == type) {
System.out.println("烤肉");
} else if (3 == type) {
System.out.println("烤鱼");
}else{
System.out.println("饿着");
}
}
switch 使用
public static void main(String[] args) {
int type = 8;
eatFood(type);
}
public static void eatFood(int type){
switch (type){
case 1:
System.out.println("火锅");
break;
case 2:
System.out.println("烤肉");
break;
case 3:
System.out.println("烤鱼");
break;
default:
System.out.println("饿着");
break;
}
}
================================================
Enumimport java.util.HashMap;
import java.util.Map;
public class TestClass {
private final static Map map;
static {
map = new HashMap<>();
map.put(1, "TYPE_1");
map.put(2, "TYPE_2");
map.put(3, "TYPE_3");
}
public static void main(String[] args) {
//map种不存在的key默认为TYPE_4
String type = map.getOrDefault(8,"TYPE_4");
EatEnum.valueOf(type).eatFood();
}
}
public interface EatFood {
void eatFood();
}
public enum EatEnum implements EatFood{
TYPE_1(){
@Override
public void eatFood() {
System.out.println("火锅");
}
},
TYPE_2(){
@Override
public void eatFood() {
System.out.println("烤肉");
}
},
TYPE_3(){
@Override
public void eatFood() {
System.out.println("烤鱼");
}
},
TYPE_4(){
@Override
public void eatFood() {
System.out.println("饿着");
}
};
}



