问题描述
publicclassEnumTest{publicstaticvoidmain(Stringagrs[]){Scannerin=newScanner(System.in);System.out.print("Enterasize:(SMALL,MEDIUM,LARGE,EXTAR_LARGE)");Stringinput=in.next().toUpperCase();Sizesize=Enum.valueOf(Size.class,input);System.out.print("size="+size);System.out.print("abbreviation="+size.getAbbreviation());if(size==Size.EXTAR_LARGE)System.out.print("Goodjob--youpaidattentiontothe_.");}}enumSize{SMALL("S"),MEDIUM("M"),LARGE("L"),EXTAR_LARGE("XL");privateSize(Stringabbreviation){this.abbreviation=abbreviation;}publicStringgetAbbreviation(){returnabbreviation;}privateStringabbreviation;}这段代码的输出结果是:Enterasize:(SMALL,MEDIUM,LARGE,EXTAR_LARGE)。它下面好几个输出语句没有执行。这是什么原因呢?请帮忙解释一下!
解决方案
解决方案二:
这要看你输入的是什么,如果是S或M,L,XL会报IllegalArgumentException如果输入SMALL或MEDIUM,LARGE,EXTAR_LARGE运行正常
解决方案三:
importjava.util.Scanner;publicclassEnumTest{publicstaticvoidmain(Stringagrs[]){Scannerin=newScanner(System.in);System.out.print("Enterasize:(SMALL,MEDIUM,LARGE,EXTAR_LARGE)");Stringinput=in.next().toUpperCase();Sizesize=Enum.valueOf(Size.class,input);System.out.print("size="+size);System.out.print("abbreviation="+size.getAbbreviation());if(size==Size.EXTAR_LARGE)System.out.print("Goodjob--youpaidattentiontothe_.Size.EXTAR_LARGE");elseif(size==Size.LARGE){System.out.print("Goodjob--youpaidattentiontothe_.Size.LARGE");}elseif(size==Size.SMALL){System.out.print("Goodjob--youpaidattentiontothe_.Size.SMALL"+Size.SMALL);}}}enumSize{SMALL("S"),MEDIUM("M"),LARGE("L"),EXTAR_LARGE("XL");privateSize(Stringabbreviation){this.abbreviation=abbreviation;}publicStringgetAbbreviation(){returnabbreviation;}privateStringabbreviation;}运行:Enterasize:(SMALL,MEDIUM,LARGE,EXTAR_LARGE)small(自己输入)size=SMALLabbreviation=SGoodjob--youpaidattentiontothe_.Size.SMALLSMALL