为什么我不能使用“继续”? Java 中的 switch 语句内?

发布于 2024-08-26 10:23:35 字数 444 浏览 13 评论 0原文

为什么下面的代码

class swi  
{
    public static void main(String[] args)  
    {  
        int a=98;
        switch(a)
        {
            default:{ System.out.println("default");continue;}
            case 'b':{ System.out.println(a); continue;}
            case 'a':{ System.out.println(a);}
        }
        System.out.println("Switch Completed");
    }
}

会报错:

在循环外继续

Why is it that the following code:

class swi  
{
    public static void main(String[] args)  
    {  
        int a=98;
        switch(a)
        {
            default:{ System.out.println("default");continue;}
            case 'b':{ System.out.println(a); continue;}
            case 'a':{ System.out.println(a);}
        }
        System.out.println("Switch Completed");
    }
}

Gives the error:

continue outside of loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

玩世 2024-09-02 10:23:35

失败是 switch 语句的标准行为,因此,在 switch 语句中使用 continue 没有意义。 continue 语句仅在 for/while/do..while 循环中使用。

根据我对您意图的理解,您可能想写:

System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
    System.out.println(a);
}

我还建议您将默认条件放在最后。

编辑:
continue 语句不能在 switch 语句内使用,这并不完全正确。 (理想情况下标记的)继续语句是完全有效的。例如:

public class Main {
public static void main(String[] args) {
    loop:
    for (int i=0; i<10; i++) {
        switch (i) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 9:
            continue loop;
        }

        System.out.println(i);
    }
}
}

这将产生以下输出:
0
2
4
6
8

Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.

Based on my understanding of your intentions, you probably want to write:

System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
    System.out.println(a);
}

I would also suggest that you place the default condition at the very end.

EDIT:
It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continue statement is entirely valid. For example:

public class Main {
public static void main(String[] args) {
    loop:
    for (int i=0; i<10; i++) {
        switch (i) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 9:
            continue loop;
        }

        System.out.println(i);
    }
}
}

This will produce the following output:
0
2
4
6
8

眼趣 2024-09-02 10:23:35

continue 语句可以在循环中使用,但不能在 switch 中使用。您可能想要的是休息

The continue-Statement may be used in loops and not in switch. What you probably want is a break.

东走西顾 2024-09-02 10:23:35

因为你在循环之外有一个 continuecontinue 用于跳转到循环的下一次迭代,跳过当前迭代的剩余部分。但该代码中没有任何循环。您想要打破 switch case 块的是关键字 break (见下文)。

也没有必要将每个 case 块放在大括号内(除非您希望在其中包含本地范围的变量)。

所以有点像这样的东西会更标准:

class swi22
{
    public static void main(String[] args)
    {
        int a=98;
        switch(a)
        {
            default:
                System.out.println("default");
                break;
            case 'b':
                System.out.println(a);
                break;
            case 'a':
                System.out.println(a);
                break;
        }
        System.out.println("Switch Completed");
    }
}

还有一种思想认为 default 条件应该始终放在最后。这不是一个要求,只是一个相当广泛使用的约定。

Because you have a continue outside of a loop. continue is for jumping to the next iteration of a loop, skipping the remainder of the current iteration. But you don't have any loop in that code. What you want for breaking out of a switch case block is the keyword break (see below).

There's also no need to put every case block within braces (unless you want locally-scoped variables within them).

So something a bit like this would be more standard:

class swi22
{
    public static void main(String[] args)
    {
        int a=98;
        switch(a)
        {
            default:
                System.out.println("default");
                break;
            case 'b':
                System.out.println(a);
                break;
            case 'a':
                System.out.println(a);
                break;
        }
        System.out.println("Switch Completed");
    }
}

There's also a school of thought that says the default condition should always be at the end. This is not a requirement, just a fairly widely-used convention.

吃不饱 2024-09-02 10:23:35

难道不应该使用 break 而不是 continue 吗?

Shouldn't you use break instead of continue?

弥繁 2024-09-02 10:23:35

continue 只是直接移动到循环的下一次迭代。

break 用于跳出循环和开关。

使用 break; 而不是 continue;

继续:

for(x = 0; x < 10; x++)
{
   if(x == 3)
     continue;
   else       
     DoIterativeWork();       
}

切换:

switch(a)
{
 default:{ System.out.println("default"); break;}
 case 'b':{ System.out.println(a); break;}
 case 'a':{ System.out.println(a);}
}

continue simply moves directly to the next iteration of the loop.

break is used to break out of loops and switches.

Use break; instead of continue;

Continue:

for(x = 0; x < 10; x++)
{
   if(x == 3)
     continue;
   else       
     DoIterativeWork();       
}

Switch:

switch(a)
{
 default:{ System.out.println("default"); break;}
 case 'b':{ System.out.println(a); break;}
 case 'a':{ System.out.println(a);}
}
放手` 2024-09-02 10:23:35

您在应该使用 break 的地方使用了 continue

You are using continue where you should be using break

少跟Wǒ拽 2024-09-02 10:23:35

在开关内继续???!!!只有 break 可以保留在 switch 内部。!

你的代码应该是,

class swi22  
{
     public static void main(String[] args)  
     {  
         int a=98;
         switch(a)
         {
             default:{ System.out.println("default");break;}
             case 'b':{ System.out.println(a); break;}
             case 'a':{ System.out.println(a);}
          }
          System.out.println("Switch Completed");
     }
}

continue inside switch??!!! only break can be kept inside switch.!

ur code should be,

class swi22  
{
     public static void main(String[] args)  
     {  
         int a=98;
         switch(a)
         {
             default:{ System.out.println("default");break;}
             case 'b':{ System.out.println(a); break;}
             case 'a':{ System.out.println(a);}
          }
          System.out.println("Switch Completed");
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文