我是否可以有一个switch
决定返回什么的声明?例如,我想根据我的随机生成器提出的内容返回不同的内容。Eclipse 给了我一个错误,希望我将return
语句放在switch
.
我的代码:
public String wordBank() { //Error here saying: "This method must return a type of string"
String[] wordsShapes = new String[10];
wordsShapes[1] = "square";
wordsShapes[2] = "circle";
wordsShapes[3] = "cone";
wordsShapes[4] = "prisim";
wordsShapes[5] = "cube";
wordsShapes[6] = "cylinder";
wordsShapes[7] = "triangle";
wordsShapes[8] = "star";
wordsShapes[9] = "moon";
wordsShapes[10] = "paralellogram";
Random rand = new Random();
int i = rand.nextInt(11);
if (i == 0) {
i = rand.nextInt(11);
}
switch (i) {
case 1:
return wordsShapes[1].toString();
case 2:
return wordsShapes[2].toString();
case 3:
return wordsShapes[3].toString();
case 4:
return wordsShapes[4].toString();
case 5:
return wordsShapes[5].toString();
case 6:
return wordsShapes[6].toString();
case 7:
return wordsShapes[7].toString();
case 8:
return wordsShapes[8].toString();
case 9:
return wordsShapes[9].toString();
case 10:
return wordsShapes[10].toString();
}
}