【问题标题】:Nested For Loop, Replace If statement嵌套 For 循环,替换 If 语句
【发布时间】:2018-03-23 23:58:35
【问题描述】:

我最近一直在练习我的嵌套 for 循环。我尝试构建一个从 (0,0) 到 (5,5) 的 5x5 坐标块。每行将上升到 y = 5,然后开始一个新行。

这是我构建的代码,它做我想做的事,唯一的问题是我必须使用 if 语句。是否有另一个 for 循环我可以用来制作它,这样我就不必使用 if 语句?

public class NestedLoops3 {
public static void main(String[] args) {

    for(int counter =0;counter<6;counter++) {
        for(int counter2 = 0;counter2<6;counter2++)
             {  
            System.out.print("("+counter+","+counter2+")");
            if(counter2==5) {
                System.out.println();
            }
    }
}
}
}

现在它符合我的预期,但我只是想看看是否可以用另一个 for 循环替换 if 语句。

【问题讨论】:

  • If 语句几乎不会给您的代码增加任何开销。它有什么问题?
  • 我只是想用 for 循环来做。我知道有可能只是卡住了。

标签: java for-loop nested-loops


【解决方案1】:

是的。在嵌套循环之后移动println。喜欢,

for (int counter = 0; counter < 6; counter++) {
    for (int counter2 = 0; counter2 < 6; counter2++) {  
        System.out.print("(" + counter + "," + counter2 + ")");
    }
    System.out.println();
}

【讨论】:

    【解决方案2】:

    在嵌套的 for 循环外调用 System.out.println();

    【讨论】:

      【解决方案3】:

      我不建议用 for 循环编写 if 语句 :)

      在循环工作后编写新行时,对于方形网格,您可以使用单个循环并计算行和列

      int size = 5;
      
      for(int counter =0;counter<(size*size)+1;counter++) {
          int col = counter % size;
          int row = counter / size;
          System.out.printf("(%d, %d) ", row, col);
          if(col == size) {
              System.out.println();
          }
      }
      

      【讨论】:

        【解决方案4】:

        是的,有可能:

        public class NestedLoops3 {
            void main(String[] args) {
                for(int counter =0;counter<6;counter++) {
                    for(int counter2 = 0;counter2<6;counter2++) {  
                        System.out.print("("+counter+","+counter2+")");
                        for (; counter2 == 5; ) {
                            System.out.println();
                            break;
                        }
                    }
                }
            }
        }
        

        但我看不出这样做的理由。 for 循环的中间部分是一个条件,可用于该目的。为了避免副作用,我放弃了将 counter2 设置为 6 例如,不使用 break 语句中断循环,但由于 counter2 的范围有限,这也是有效的:

                    for(int counter2 = 0;counter2<6;counter2++) {  
                        System.out.print("("+counter+","+counter2+")");
                        for (; counter2 == 5; counter2=6) {
                            System.out.println();
                        }
                    }
        

        由于这种风格非常少见,很容易出错,如果有人尝试扩展代码,例如将中间循环设置为运行 7。

        但有可能,是的。

        输出:

        -> main (null) 
        (0,0)(0,1)(0,2)(0,3)(0,4)(0,5)
        (1,0)(1,1)(1,2)(1,3)(1,4)(1,5)
        (2,0)(2,1)(2,2)(2,3)(2,4)(2,5)
        (3,0)(3,1)(3,2)(3,3)(3,4)(3,5)
        (4,0)(4,1)(4,2)(4,3)(4,4)(4,5)
        (5,0)(5,1)(5,2)(5,3)(5,4)(5,5)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-09
          • 2014-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多