如何在java代码中使用标签? [英] How to use labels in java code?

查看:756
本文介绍了如何在java代码中使用标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java代码中的标签中使用break语句时遇到错误。这显示未定义标签。编写这样的代码是错误的。请帮我正确使用它。提前致谢。

I am getting error while using break statements in labels in java code. This is showing undefined label. It is wrong to write code like this. Please assist me in using it correctly. Thanks in advance.

 while (true)
          {
            label149: if (!localIterator2.hasNext());
            while (true)
            {
              i++;
              break;
              HashMap localHashMap2 = (HashMap)localIterator2.next();
              if (!((String)localHashMap1.get("name")).equalsIgnoreCase((String)localHashMap2.get("emotion")))
                break label149;
              if (!((String)localHashMap2.get("IS_paid")).equalsIgnoreCase("1"))
                break label246;
              ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "1");
            }
            label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");
          }


推荐答案

A break 标签与 goto 语句不同。 Java没有 goto 语句。

A break with a label is not the same as a goto statement. Java does not have a goto statement.

标签标记后面的语句。您可以使用它来突破该语句,并且只能在该语句之外。流量控制将始终转移到标签声明的末尾。

A label marks the statement that follows it. You can use it to break out of that statement, and only out of that statement. Control of flow will always transfer to the end of the labeled statement.

那你在这里有什么?

        label149: if (!localIterator2.hasNext());

由于if之后的分号,这实际上是整个带标签的语句。所以你的 break label149 根本不在其范围内。如果你没有那个分号, if 块将包含,然后是 break label149 可行。但是控制权将在结束后转移到该行,而块。

Because of the semicolon after the if, this is in fact the entire labeled statement. So your break label149 is not within its scope at all. If you did not have that semicolon, the if block would include the while, and then the break label149 would work. But control would be transferred to the line after the end of the while block.

       label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");

这是label246标记的声明。同样, break label246 不在其中,因此它不在其范围内,并且你不能打破你不在其中的声明。

This is the statement marked by label246. Again, the break label246 is not inside it, so it is not in its scope, and you can`t break out of a statement you are not inside of.

这篇关于如何在java代码中使用标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆