简体   繁体   English

为什么不休息

[英]Why doesn't this break

My code below is from Oracle. 我下面的代码来自Oracle。

public class MyLoop {
  public static void main(String[] args) { 
    String[] sa = {"tom ", "jerry "}; 
    for(int x = 0; x < 3; x++) {
      for(String s : sa) {
        System.out.print(x + " " + s);
        if(x == 1) break; 
      }
    }
  }
}

Output: 输出:

 0 tom 0 jerry 1 tom 2 tom 2 jerry 

I am learning java and I came across this. 我正在学习Java,并且遇到了这个问题。 I don't understand why 1 tom prints when the break is at 1 . 我不明白为什么中断点为1时会打印1 tom If 1 tom prints why then doesn't 1 jerry ? 如果打印了1 tom ,那么为什么没有1 jerry

对于x每个值,您正在打印表sa的全部内容,但x == 1除外,其中仅打印sa的第一个值:打印此值后,您要检查x == 1以及然后离开内部循环并继续x的下一个值。

First check "x == 1" then print. 首先检查“ x == 1”,然后打印。

String[] sa = { "tom ", "jerry " };
for (int x = 0; x < 3; x++) {

    for (String s : sa) {
        if (x == 1) {
          break;
        }
        System.out.print(x + " " + s);

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM