java break循环不起作用

时间:2014-12-24 09:28:25

标签: java android breakpoints

我的java代码中有一个中断标签但是当我在代码中转到break语句时它没有跳转到标签:

  OUTERMOST:for(String s :soso){

        if(wasBreaked){
              //code never enters this loop
                Log.e("","WASBREAK = FALSE");
                wasBreaked = false;
        }

        if(true){
                Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
                wasBreaked = true;
                break OUTERMOST;
        }
  }

2 个答案:

答案 0 :(得分:0)

Break语句实际上并不像其他编程语言那样是goto语句,如C.

你的代码所做的是,从具有标签OUTERMOST的循环中断。我本以为你需要continue OUTERMOST;而不是休息。但是对我来说这真的没有意义,因为你没有进一步的任何声明后继续(现在你的代码中断)并且无论你是否明确表示继续,它都会继续。

答案 1 :(得分:0)

public class Test {

   public static void main(String[] args) {

    String soso[]={"1","2"};
    boolean wasBreaked=false;
    OUTERMOST:for(String s :soso){

        if(wasBreaked){
                Log.e("","WASBREAK = FALSE");
                wasBreaked = false;
        }

        if(true){
                Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
                wasBreaked = true;
                System.out.println("before break");

                break OUTERMOST;//use continue in place of break here for going to label
                }
        System.out.println("Inside outermost loop");
}
    System.out.println("outside outermost loop");

}

}

我试过这个并且它有效 它给出了输出

   before break
   outside outermost loop

要返回标签,您应该在代码中使用继续关键字而非中断。使用后继续输出就像

 before break
 before break
 outside outermost loop