教材裡面有段關於枚舉的代碼,但一直報錯,麻煩高手看一下
import java.util. *;
public class EnumTest {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("請輸入服裝型號:(SMALL,MEDIUM,LARGE,EXTRA_LARGE,EXTRA_EXTRA_LARGE)");
String input=in.next().toUpperCase();
Size size=Enum.valueOf(Size.class,input);
System.out.println("Size="+size);
System.out.println("abbreviation="+size.getAbbreviation());
if(size==Size.EXTRA_LARGE^size==Size.EXTRA_EXTRA_LARGE)
System.out.println("很好,你注意到了下劃線");
}
public static String abbreviation;
enum Size{SMALL,MEDIUM,EXTRA_LARGE,EXTRA_EXTRA_LARGE}
private void Size(String abbreviation){this.abbreviation=abbreviation;}
public String getAbbreviation(){return abbreviation;}
}
像這樣:
enum Size{SMALL,MEDIUM,EXTRA_LARGE,EXTRA_EXTRA_LARGE;
public String getAbbreviation() {
// TODO Auto-generated method stub
return abbreviation;
}
}