Ber*_*ion 3 java return switch-statement
public AlertStatus nextStatus(){
int randNum = randNumGen.nextInt(3);
switch(randNum){
case 0: return new AlertStatusGreen();
case 1: return new AlertStatusYellow();
case 2: return new AlertStatusRed();
default: System.out.println("ERROR: no random number.");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我必须为学校准备的课程的一种方法。开关采用随机整数,并使用它返回从AlertStatus类派生的某个特定类的对象。
由于某种原因,我在上面的代码块的第9行(上面的代码的最后一行)中始终收到错误消息“缺少返回语句}”。我不明白为什么这样说,尽管我已经看到每种情况的返回语句。
在这种default情况下,您什么也不会退货。我想你想要类似的东西
public AlertStatus nextStatus(){
int randNum = randNumGen.nextInt(3);
switch(randNum){
case 0: return new AlertStatusGreen();
case 1: return new AlertStatusYellow();
default: return new AlertStatusRed();
// default: System.out.println("ERROR: no random number.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3779 次 |
| 最近记录: |