public class RandomInstrumentGenerator {
private Random rand = new Random();
public Instrument next() {
switch (rand.nextInt(6)) {
case 0:
return new Wind(); // return会直接跳出循环,不需要break
case 1:
return new Percussion();
case 2:
return new Stringed();
case 3:
return new Brass();
case 4:
return new Woodwind();
case 5:
return new Piano();
default:
}
}
}
public class RandomInstrumentGenerator {
private Random rand = new Random();
public Instrument next() {
switch (rand.nextInt(6)) {
default:
case 0:
return new Wind(); // return会直接跳出循环,不需要break
case 1:
return new Percussion();
case 2:
return new Stringed();
case 3:
return new Brass();
case 4:
return new Woodwind();
case 5:
return new Piano();
}
}
}
当default放最后时还会报错: Missing return statement