java 我可以将 return 语句放在 switch 语句中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17961047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:43:24  来源:igfitidea点击:

Can I put a return statement inside a switch statement?

javamethodsswitch-statementreturn

提问by Col1107

Am I allowed to have a switchstatement 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 returnstatement outside the switch.

我可以有一个switch决定返回什么的声明吗?例如,我想根据我的随机生成器提出的内容返回不同的内容。Eclipse 给我一个错误,希望我将return语句放在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();
    }
}

采纳答案by Ramon Araujo

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,

希望有所帮助,

回答by fastcodejava

You can put returninside switchbut you don't need to use switchin this case.

可以放在return里面,switchswitch在这种情况下不需要使用。

回答by Ray Toal

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

问题不在于您在语句中具有 return 语句switch,这完全没问题,但是在 switch 语句之后没有 return 。如果您的 switch 语句完成而没有返回,现在会发生什么?

The rules of Java require that all paths through a value-returning function encounter a returnstatement. In your case, even though youknow the value of iwill always be of a value that will cause a returnfrom the switch, the Java compiler isn't smart enough to determine that.

Java 的规则要求通过返回值函数的所有路径都遇到一个return语句。在您的情况下,即使知道iof 的值始终是一个会导致 a returnfrom 开关的值,但 Java 编译器不够聪明,无法确定这一点。

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

(旁白:顺便说一句,您实际上并没有阻止生成值 0;也许您if应该是while.)

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

附录:如果您有兴趣,这里有一个实现。有关实时示例,请参阅http://ideone.com/IpIwis

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.

请注意在函数外部定义随机数生成器的最佳实践。

回答by Vbalaji21

The return statement will return from the entire function in which it is used.So I think it is good that no other useful lines of code must be there below the switch if you want to use the return statement in the switch.

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