我想講一個小動物的程序,從四個選項中隨機選擇攻擊類型;我原本以爲我的return語句基地覆蓋,但我碰到一個drjava「失蹤return語句」錯誤:缺少return語句錯誤的if-then樹
public Attack fight(String opponent) {
int fightChoice = new Random().nextInt(4);
if(fightChoice == 0){
return Attack.ROAR;
} if(fightChoice == 1){
return Attack.POUNCE;
} if(fightChoice == 2){
return Attack.SCRATCH;
} if(fightChoice == 3){
return Attack.FORFEIT;
}
}
任何想法,爲什麼這可能是?
這是使用[switch語句](http://www.leepoint.net/notes-java/flow/switch/switch-general.html)的好例子。 –