【问题标题】:Nested for-loop嵌套for循环
【发布时间】:2014-03-24 05:49:49
【问题描述】:

这段代码让我很头疼

    for (int i = 0; i<10; i++)
    {
          for (int c = 0; c < 10; c++)                  
              if(i == c)
                  System.out.print("*");
              else
                  System.out.print("-");

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

我知道内部 for 循环必须在外部循环再次循环之前完成。为什么 System.out.println(".") 在内循环中只出现一次,即使它应该循环多次?

【问题讨论】:

  • 你应该使用 else{ ----code----} 打印多行,否则只需要一行。
  • Protip:总是使用带有流控制结构的大括号。 (在你成为专业人士之前,请注意前面的声明。)

标签: java for-loop nested-loops


【解决方案1】:

线

System.out.println(".");

在嵌套的for 之外。如果你想在里面,你应该使用括号{}

for (int i = 0; i < 10; i++) {
    for (int c = 0; c < 10; c++) {
        if (i == c) {
            System.out.print("*");
        } else {
            System.out.print("-");
        }
        System.out.println(".");
    }
}

请记住,如果您不使用方括号 {}for 循环的主体将只有一个语句(与 for 声明相邻的语句)。

【讨论】:

    【解决方案2】:

    因为它在内部 for 循环之外,所以在 for 循环体周围使用括号以提高可读性并避免这种混淆

    【讨论】:

      【解决方案3】:

      不确定您的预期,但您的代码相当于:

      for (int i = 0; i<10; i++)
      {
            for (int c = 0; c < 10; c++) {
                if(i == c) {
                    System.out.print("*");
                } else {
                    System.out.print("-");
                }
            }
      
            System.out.println("."); // This is OUTSIDE the inner loop.
      }
      

      故事的寓意:如有疑问,请使用括号。

      【讨论】:

        【解决方案4】:

        内部循环必须完成所有的通过/次数,而不是在再次运行父循环之前完成一次。

        【讨论】:

          【解决方案5】:
          for (int i = 0; i<10; i++)
          {
                for (int c = 0; c < 10; c++)  
                    {            
                    if(i == c)
                        System.out.print("*");
                    else
                        System.out.print("-");
                    }
              System.out.println(".");//The . would get printed 10 times as it is inside first for loop.
          }
          

          【讨论】:

          • 这是什么意思?包括字词。
          【解决方案6】:

          这种混乱是因为没有花括号。您的代码没有任何问题。最后一个让您感到困惑的System.out.println("."); 是外循环的一部分。所以你的代码相当于这个:

          for (int i = 0; i<10; i++)
          {
                for (int c = 0; c < 10; c++) {                 
                    if(i == c){
                        System.out.print("*");
                    }
                     else{
                        System.out.print("-");
                    }
              System.out.println(".");
              }
          }
          

          【讨论】:

            【解决方案7】:

            您的最终语句是外循环的一部分。

            System.out.println(".");

            由于内部循环中有一个 if-else 语句,因此不需要大括号。 总是使用大括号来定义循环的范围并为了可读性。

            for (int i = 0; i<10; i++)
            {
                  for (int c = 0; c < 10; c++) {                 
                      if(i == c) {
                          System.out.print("*");
                      } 
                      else {
                          System.out.print("-");
                      } 
                  }
            
                System.out.println(".");
            }
            

            【讨论】:

            • 您说过“始终使用大括号”,然后将它们排除在 if/else 之外。如果代码 sn-p 符合建议,这个答案会更好。
            • 如果提供了大括号,我的目的是显示问题中给出的代码将是什么样子。
            【解决方案8】:

            您代码中的 Sysout 属于外循环,而不是内循环。

            如果你没有在你的 for 循环后面加上花括号,那么它后面只需要 1 条语句。

            这应该适用于您的情况。

            for (int i = 0; i<10; i++)
            {
                  for (int c = 0; c < 10; c++)                  
                   {   if(i == c)
                          System.out.print("*");
                      else
                          System.out.print("-");
            
                System.out.println(".");}
                 }
             }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-01-28
              • 2021-07-08
              • 2012-06-27
              • 2016-05-07
              • 2014-09-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多