嵌套循环:用 Java 打印座位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28758232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nested loops: Print seats in java
提问by Gui
Given numRows and numCols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numCols = 3 prints: 1A 1B 1C 2A 2B 2C
给定 numRows 和 numCols,打印剧院中所有座位的列表。行有编号,列有字母,如 1A 或 3E。在每个座位之后打印一个空格,包括最后一个之后。使用单独的打印语句打印行和列。例如:numRows = 2 和 numCols = 3 打印:1A 1B 1C 2A 2B 2C
My code's like:
我的代码是这样的:
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;
}
}
And my output's like:
我的输出是这样的:
1A 1B 1C
I tried to make it like:
我试着让它像:
1A 1B 1C 2A 2B 2C
Why my loop stop at 1?
为什么我的循环在 1 处停止?
采纳答案by Parasu
add a line cols = 1;
just after rows++;
cols = 1;
在后面添加一行rows++;
回答by Chris Jester-Young
You didn't reset the value of cols
in the outer loop. So the second time through the outer loop, the inner loop never gets run at all. Also consider using a for
loop:
您没有重置cols
外循环中的值。所以第二次通过外循环时,内循环根本不会运行。还可以考虑使用for
循环:
for (int rows = 0; rows < numRows; ++rows) {
// ...
for (int cols = 0; cols < numCols; ++cols) {
// ...
}
}
回答by TSKSwamy
Just change the position of cols = 1;
只需改变位置 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(" ");
}
回答by lomsansnom
On the first iteration, when it goes over the inner while, you update the value of "cols". So when this while is done, cols = 3.
在第一次迭代中,当它越过内部 while 时,更新“cols”的值。所以当这个while完成时,cols = 3。
Then on the second iteration, you still have cols=3 which is > numCols so it doesn't execute the while loop.
然后在第二次迭代中,您仍然有 cols=3,即 > numCols,因此它不会执行 while 循环。
As said Parasu just add "cols = 1;" before the inner loop and it'll works.
正如 Parasu 所说,只需添加“cols = 1;” 在内循环之前,它会起作用。
回答by jam
This uses a for loop
这使用了一个 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;
}
}
回答by Cosmopus
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;
}
}
回答by user8971882
You have to just make a small change in your code. After the inner while loop but just inside the outer while loop write cols=1;... because the next time when the loop reaches the same inner while loop cols have to again start at 1 and not at some other value .
您只需对代码进行一些小的更改。在内部 while 循环之后但在外部 while 循环内部写入 cols=1;... 因为下一次当循环到达相同的内部 while 循环时 cols 必须再次从 1 开始,而不是从其他值开始。
回答by RobertB922
I know this is late, but I am going through this same challenge in zybooks. My initial code is a little different and the answers above were a little off from what the IDE was testing for. So I decided to copy and paste my code in case anyone needs a little help with this challenge.
我知道这已经晚了,但我正在 zybooks 中经历同样的挑战。我的初始代码有点不同,上面的答案与 IDE 测试的内容略有不同。所以我决定复制并粘贴我的代码,以防有人需要帮助来解决这个挑战。
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(); // user input for how many rows
numColumns = scnr.nextInt(); // user input for many columns
/* Your solution goes here */
currentRow = 0; // Must be intialized to 0 or a "2" will print before "1"
while (currentRow < numRows) { // Intialized the loop
currentRow++; // increments the currentRow
currentColumnLetter = 'A'; // Sets the first column to char 'A'
currentColumn = 1; // Define to intialize inner loop
while (currentColumn <= numColumns) { // Will initial for any positive input 1 +
System.out.print(currentRow); // Asked specifically for 2 printouts
System.out.print(currentColumnLetter + " "); // 2nd printout with space at end.
currentColumnLetter++; // increments the Letter
currentColumn++; // increments the column
}
}
System.out.println("");
}
}
I hope this helps anyone needing a little help with this assignment.
我希望这可以帮助任何需要帮助完成这项任务的人。
回答by TimRCM
Here's what I came up with for this particular ZyBooks challenge (I used my own iterables instead of their pre-defined ones out of habit):
这是我为这个特殊的 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("");
}
}