在 switch 语句的默认情况下继续

Having a continue in a default case in a switch statement

如果我有这个:

do {
    int x = scannerScan.nextInt();

    switch(x)
    {
        case 1:
            System.out.println("Stuff");
            break;
        case 2:
            System.out.println("Pink cows are fluffy and can fly.");
        default:
            continue;
    }
}
while(true);

如果达到默认情况会怎样?我试图在 Internet 和 Whosebug 上查找资料,但在与 Java 语言有关的默认情况下找不到有关继续的任何信息。

continue语句中的switch语句并不特殊。它会跳转到循环条件(循环体的末尾),就像它在循环内但在 switch.

之外一样。

在这个特定的代码片段中,它实际上什么都不做。

continue循环中的语句

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]

在您的代码中,循环 while(true); 将继续。 该语句对 switch 代码块没有影响。

比较break statement:

A break statement attempts to transfer control to the innermost enclosing switch, while, do, or for statement …

continue statement:

A continue statement attempts to transfer control to the innermost enclosing while, do, or for statement …

因此,continue 指的是 do...while 循环,并且:

… then immediately ends the current iteration and begins a new one.