嵌套循环:在java中打印席位

时间:2015-02-27 05:35:11

标签: java nested-loops

给出numRows和numCols,打印剧院中所有座位的列表。行编号,列字母,如1A或3E中所示。在每个座位后打印一个空格,包括在最后一个之后。使用单独的print语句打印行和列。例如:numRows = 2和numCols = 3打印: 1A 1B 1C 2A 2B 2C

我的代码就像:

public static void main(String[] args) {
    int numRows = 2;
    int numCols = 3;
    int rows = 0;
    int cols = 1;
    char col;

    while (rows < numRows) {

        rows++;

        col='A';

        while (cols <= numCols) {
            System.out.print("" + rows + col + " ");
            col++;
            cols++;
        }

    }
    System.out.println(" ");

    return;

}

}

我的输出就像:

1A 1B 1C 

我试着让它像:

1A 1B 1C 2A 2B 2C

为什么我的循环停在1?

10 个答案:

答案 0 :(得分:1)

您没有在外部循环中重置cols的值。所以第二次通过外部循环,内部循环根本不会运行。另请考虑使用for循环:

for (int rows = 0; rows < numRows; ++rows) {
    // ...
    for (int cols = 0; cols < numCols; ++cols) {
        // ...
    }
}

答案 1 :(得分:0)

cols = 1;

之后添加一行rows++;

答案 2 :(得分:0)

只需更改cols = 1;

的位置即可
public static void main(final String[] args){
        int numRows = 2;
        int numCols = 3;
        int rows = 0;


        while (rows < numRows) {

            rows++;
            int cols = 1;
            char col = 'A';

            while (cols <= numCols) {
                System.out.print("" + rows + col + " ");
                col++;
                cols++;
            }
        }
        System.out.println(" ");
}

答案 3 :(得分:0)

在第一次迭代中,当它越过内部时,你更新&#34; cols&#34;的值。 所以当这一刻完成时,cols = 3。

然后在第二次迭代中,你仍然有cols = 3,这是&gt; numCols所以它不会执行while循环。

正如Parasu所说,只需添加&#34; cols = 1;&#34;在内循环之前,它会起作用。

答案 4 :(得分:0)

这使用for循环

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


    int numRows = 2;
      int numCols = 3;


      int i = 0;
      int j = 0;
      char rows;
      char columns;

      rows = '1';
      for (i = 0; i < numRows; ++i) {
         columns = 'A';
         for (j = 0; j < numCols; ++j) {
            System.out.print("" + rows + columns + " ");
            columns++;
         }
         rows++;
      }


      System.out.println("");

      return;
   }
}

答案 5 :(得分:0)

public class NestedLoops_2 {
       public static void main (String [] args) {
          int numRows = 2;
          int numCols = 5;

for (int i = 1; i <= numRows; i++){ 
          char abc = 'A';
          for (int j = 1; j <= numCols; ++j) {
          System.out.print("" + i + abc + " ");
          ++abc;     
          }
    }
          System.out.println("");
          return;
       }
    }

答案 6 :(得分:0)

您只需对代码进行一些小改动即可。在内部while循环之后但只是在外部while循环内部写cols = 1; ...因为下一次当循环到达同一内部时,循环cols必须再次从1开始而不是在某个其他值。

答案 7 :(得分:0)

我知道这很晚了,但是我在zybook中正经历着同样的挑战。我的初始代码略有不同,并且上面的答案与IDE所测试的略有不同。因此,我决定复制并粘贴我的代码,以防有人需要对此挑战有所帮助。

EventArgs

我希望这对需要一点帮助的人有所帮助。

答案 8 :(得分:0)

这是我针对此特定ZyBooks挑战提出的(出于习惯,我使用了自己的可迭代对象而不是它们的预定义对象):

    import java.util.Scanner;
    public class NestedLoops {
    public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      int numRows;
      int numColumns;
      int currentRow;
      int currentColumn;
      char currentColumnLetter;

      numRows = scnr.nextInt();
      numColumns = scnr.nextInt();

      for (int i = 1; i <= numRows; i++) {

         currentColumnLetter = 'A';

         for (int x = 1; x <= numColumns; x++) {
            System.out.print(i);
            System.out.print(currentColumnLetter + " ");
            currentColumnLetter++;
         }

      }

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

答案 9 :(得分:0)

import java.util.Scanner;
public class NestedLoops {
   public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      int numRows;
      int numColumns;
      int currentRow;
      int currentColumn;
      char currentColumnLetter; //not needed

      numRows = scnr.nextInt();
      numColumns = scnr.nextInt();
      String currentColumnL;
      char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
      for(currentRow = 1; currentRow <= numRows; currentRow = currentRow + 1) {
         for(currentColumn = 1; currentColumn <= numColumns; currentColumn = currentColumn + 1) {
            currentColumnL = Character.toString(alphabet[currentColumn-1]);
            System.out.print("" + currentRow + currentColumnL + " ");
   }
}

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

这是我对4.7.2的解决方案:嵌套循环:打印座位,我没有使用char currentColumnLetter