Java 问题:是否可以在另一个语句中包含一个 switch 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1583744/
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-08-12 16:28:48  来源:igfitidea点击:

Java Question: Is it possible to have a switch statement within another one?

javaswitch-statement

提问by HelloWorld

I have a yes or no question & answer. I would like to ask another yes or no question if yes was the answer. My instructor wants us to use charAt(0) as input for the answer.

我有一个是或否的问题和答案。如果答案是肯定的,我想问另一个是或否的问题。我的老师希望我们使用 charAt(0) 作为答案的输入。

Is it possible to have a switch statement within another one (like a nested if statement)?

是否可以在另一个语句中使用 switch 语句(如嵌套的 if 语句)?

EDIT: Here is a sample of my pseudo code =

编辑:这是我的伪代码示例 =

display "Would you like to add a link (y = yes or n = no)? "
input addLink

switch (link)
    case 'y':
        display "Would you like to pay 3 months in advance " + "(y = yes or n = no)?"
        input advancePay

                switch(advPay)
                        case 'y':
                linkCost = 0.10 * (3 * 14.95)

                case 'n'
                linkCost = 14.95            
    case 'n'
        linkCost = 0.00

采纳答案by Ande Turner

Yes, but don't. If you need a further level of logic like that, place the second Switch in its own method with a descriptive name.

是的,但不要。如果您需要更深层次的逻辑,请将第二个 Switch 放在其自己的具有描述性名称的方法中。

EDIT: By the example you've added, you've got two Boolean conditions. I would recommend against using switch at all, using if& elseconditionals instead. Use (STRING.charAt(0) == 'y')as your test case, or something methodical like boolean isY(final String STRING) { return (STRING.charAt(0) == 'y' || STRING.charAt(0) == 'Y'); }

编辑:通过您添加的示例,您有两个布尔条件。我建议完全不要使用 switch,而是使用if&else条件。使用(STRING.charAt(0) == 'y')作为测试的情况下,什么样有条不紊boolean isY(final String STRING) { return (STRING.charAt(0) == 'y' || STRING.charAt(0) == 'Y'); }

回答by Kaleb Brasee

It is possible, but that would be very convoluted and hard to read. There's almost certainly a better way, such as calling a method within the first switch statement and doing any more necessary processing (including another switch if necessary) in that method.

这是可能的,但这会非常复杂且难以阅读。几乎可以肯定有更好的方法,例如在第一个 switch 语句中调用一个方法并在该方法中执行任何更多必要的处理(包括另一个 switch,如果需要)。

回答by srini.venigalla

Most programming languages are orthogonal. That means, using a feature usually does not depend on the location, if meaningful.

大多数编程语言都是正交的。这意味着,如果有意义的话,使用特征通常不依赖于位置。

For example, you cannot declare a local variable as public.

例如,您不能将局部变量声明为公共变量。

回答by MetroidFan2002

Yes. Switches break the language block statement pattern, but this is mainly because of C/C++ from which the switch statement used by Java is based.

是的。Switch 打破了语言块语句模式,但这主要是因为 Java 使用的 switch 语句基于 C/C++。

In all three languages, the switch statement takes the following form:

在所有三种语言中,switch 语句采用以下形式:

switch(variable) {
     case n:
        statement 1;
        statement n;
        (optional) break;
     case n+1:
        statement 1;
        statement n;
        (optional) break;
     // Optionally more statements
     (optional) default:
        statement 1;
        statement n;
}

Because a switch statement breaks the traditional language pattern, many programmers wrap their multiple statements in a case using the traditional block style: { }

因为 switch 语句打破了传统的语言模式,许多程序员使用传统的块样式将他们的多条语句包装在一个 case 中:{ }

This is because most constructs in all three languages allow block style statements to be considered as one statement, but the switch statement does not require block style to execute multiple statements in a single case.

这是因为所有三种语言中的大多数构造都允许将块样式语句视为一个语句,但 switch 语句不需要块样式在单个 case 中执行多个语句。

Without the break statement separating each case, there will be "fall through" - if case n was matched and did not have a break, the code under it (case n + 1) would be executed - if case n + 1 did not have a break and was matched, the default code would execute, if neither had a break, when matching case n, the code for case n, case n+1 and default would be executed.

如果没有分隔每个 case 的 break 语句,将会有“fall through”——如果 case n 匹配并且没有 break,则它下面的代码(case n + 1)将被执行 - 如果 case n + 1 没有一个中断并被匹配,默认代码将执行,如果没有中断,当匹配案例 n 时,将执行案例 n、案例 n+1 和默认的代码。

The default is optional, and specifies a default action for a switch statement to execute. Typically, the default condition is either a generic handler, or a good place to throw an exception if the value could not logically be any other than the values in the switch statement.

默认值是可选的,并指定要执行的 switch 语句的默认操作。通常,默认条件要么是通用处理程序,要么是在逻辑上不能是 switch 语句中的值以外的任何值时抛出异常的好地方。

To illustrate a switch statement executing inside a switch statement, take a look at this contrived example:

为了说明在 switch 语句中执行的 switch 语句,请看这个人为的示例:

String message = null;
int outerVariable = getOuterVariable();
switch(outerVariable) {
     case n:
        statement 1;
        statement n;
        break;
     case n+1:
        int innerVariable = getInnerVariable();
        switch(innerVariable) {
            case 1:
                message = "IT WAS 1";
                break;
            default:
                message = "WHY WOULD YOU DO THIS?  OH THE HUMANITY!";
        }
        break;
     // Optionally more statements
     (optional) default:
        statement 1;
        statement n;
}