简体   繁体   中英

Can I put a return statement inside a switch statement?

Am I allowed to have a switch statement which decides what to return? For example, I want to return something different based on what my random generator come up with. Eclipse is giving me an error wanting me to put the return statement outside the switch .

My code:

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();
    }
}

Sorry, but in that case, why not just simply do:

return wordsShapes[i].toString();

This way you can avoid the switch and all.

Hope that helps,

你可以把return放在switch但在这种情况下你不需要使用switch

The problem is not that you have return statements inside the switch statement, which are perfectly fine, but you have no return after the switch statement. If your switch statement completes without returning, what will happen now?

The rules of Java require that all paths through a value-returning function encounter a return statement. In your case, even though you know the value of i will always be of a value that will cause a return from the switch, the Java compiler isn't smart enough to determine that.

(ASIDE: By the way, you didn't actually prevent the value 0 from being generated; maybe your if should be a while .)

ADDENDUM: In case you are interested, here's an implementation. See http://ideone.com/IpIwis for the live example.

import java.util.Random;
class Main {
    private static final Random random = new Random();

    private static final String[] SHAPES = {
        "square", "circle", "cone", "prism", "cube", "cylinder", "triangle",
        "star", "moon", "parallelogram"
    };

    public static String randomShapeWord() {
        return SHAPES[random.nextInt(SHAPES.length)];
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            System.out.println(randomShapeWord());
        }
    }
}

Note the best practice of having the random number generator defined outside the function.

return语句将从使用它的整个函数返回。所以我认为如果你想在switch中使用return语句,那么在交换机下面不能有其他有用的代码行是很好的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM