给定 numRows 和 numCols,打印剧院所有座位的列表。行有编号,列有字母,如 1A 或 3E。在每个座位后打印一个空格,包括在最后一个座位后。使用单独的打印语句打印行和列。例如: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?
这是我为这个特殊的 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("");
}
}
在
cols = 1;
之后添加一行
rows++;
你没有在外循环中重置
cols
的值。所以第二次通过外循环时,内循环根本就不会运行。还可以考虑使用 for
循环:
for (int rows = 0; rows < numRows; ++rows) {
// ...
for (int cols = 0; cols < numCols; ++cols) {
// ...
}
}
在第一次迭代中,当它越过内部 while 时,您更新“cols”的值。 所以当这个 while 完成时,cols = 3.
然后在第二次迭代中,您仍然有 cols=3 这是 > numCols 所以它不会执行 while 循环。
正如 Parasu 所说,只需添加“cols = 1;”在内循环之前它会起作用。
这使用了一个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;
}
}
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;
}
}
你只需要对你的代码做一点小改动。在内部 while 循环之后但就在外部 while 循环内部写入 cols=1;... 因为下次循环到达相同的内部 while 循环时,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(" ");
}
我知道这已经晚了,但我正在 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("");
}
}
我希望这可以帮助任何需要一些帮助的人。
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
这是我的解决方案。这个真的很难!
需要指出的一点是,您不能在同一打印语句中打印字符串和字符。我不确定为什么,但如果你这样做它就不会运行。
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(currentRow = 1; currentRow <= numRows; currentRow++){
// Set the starting column letter
currentColumnLetter = 'A';
for(currentColumn = 0; currentColumn < numColumns; currentColumn++){
System.out.print(currentRow);
System.out.print(currentColumnLetter + " ");
currentColumnLetter++;
}
}
System.out.println("");
}
}
这是我在课堂上使用 C++ 得出的答案:
#include <iostream>
using namespace std;
int main() {
int numRows;
int numColumns;
int currentRow;
int currentColumn;
char currentColumnLetter;
cin >> numRows;
cin >> numColumns;
for (currentRow = 1; currentRow <= numRows; currentRow++) {
currentColumnLetter = 'A';
for (currentColumn = 1; currentColumn <= numColumns; currentColumn++) {
cout << currentRow;
cout << currentColumnLetter << " ";
currentColumnLetter++;
}
}
cout << endl;
return 0;
}
希望对大家有帮助
通过 ZyBooks 中的所有测试
numRows = scnr.nextInt(); numColumns = scnr.nextInt();
for (currentRow=1; currentRow<=numRows; currentRow++){
currentColumnLetter='A';
for (currentColumn=0; currentColumn<numColumns; currentColumn++){
System.out.print(currentRow);
System.out.print(currentColumnLetter+(" "));
currentColumnLetter++;
}