我正在上在线课程,这使得获得帮助变得更加困难,这就是为什么我在这里。本周的讲座是关于嵌套循环的。他们把我弄糊涂了。我目前正陷在这个问题上。
给出numRows和numColumns,打印剧院中所有座位的列表。行编号,列字母编号,如1A或3E。在每个座位之后(包括最后一个座位之后)打印一个空格。使用单独的打印语句来打印行和列。例如:numRows = 2和numColumns = 3次打印:
1A 1B 1C 2A 2B 2C >
我尝试了许多可能的解决方案,这些解决方案产生了许多错误的结果。这是我目前的解决方案
int numRows;
int numColumns;
int currentRow;
int currentColumn;
char currentColumnLetter;
numRows = scnr.nextInt();
numColumns = scnr.nextInt();
currentColumnLetter = 'A';
for (currentRow = 1; currentRow <= numRows; currentRow++)
{
for (currentColumn = 1; currentColumn < numColumns; currentColumn++)
{
System.out.print(currentRow);
System.out.print(currentColumnLetter + " ");
}
if( currentColumn == numColumns)
{
currentColumnLetter++;
}
}
代码会产生此结果
1A 1A 2B 2B
所需的结果是
1A 1B 2A 2B
我已经做了两天了,这让我感到非常沮丧。预先感谢您的帮助。
答案 0 :(得分:1)
您非常接近。
但是,您没有正确处理列名。每行开始时,您需要返回到A
,并在每一列中加一:
for (currentRow = 1; currentRow <= numRows; currentRow++) {
currentColumnLetter = 'A'; //Starting a new row, reset the column to `A`
for (currentColumn = 1; currentColumn < numColumns; currentColumn++){
System.out.print(currentRow);
System.out.print(currentColumnLetter + " ");
currentColumnLetter++;
}
}
使用基于1的索引也很奇怪。 Java(以及许多其他语言)的索引应从0开始。结果,您的列循环执行的迭代次数不足-如果将numColumns
设置为2,则只会打印一列。
写这样的循环会更惯用:
for (currentRow = 0; currentRow < numRows; currentRow++) {
currentColumnLetter = 'A';
for (currentColumn = 0; currentColumn < numColumns; currentColumn++) {
System.out.print(currentRow + 1);
System.out.print(currentColumnLetter + " ");
currentColumnLetter++;
}
}
答案 1 :(得分:1)
是的,这件事比人们想象的要深。我总是发现这些在角色示例上的循环很讨厌,我在上一次面试时就明白了。所以我们开始:
如果练习是这样的:从1..n获得的行,从1..n获得的列,输出应为“ 1/1,1/2 ... 3/1,3/2 ... “,这很简单,不是吗?
public void seatnumbersNumbersMappedAgain() {
String[] seatLetters = new String []{"A", "B", "C"}; // so many letters as there are columns. Note: array index is 0-based
int numRows = 3;
int numColumns = 3;
for (int currentRow = 1; currentRow <= numRows; currentRow++) {
for (int currentColumn = 1; currentColumn < numColumns; currentColumn++) {
// seatLetters[i] is a string "s", not a char 'c', so everything's fine
System.out.print(seatLetters[currentColumn - 1] + currentRow + " "); // -1: seatLetters indexing is zero based
}
}
但是任务是不同的,他们想要字母作为列。让我们在相同的基础上强行使用它。
@Test
public void charByteEquivalence() {
// check out http://www.asciitable.com/
char ca = 'A';
byte ba = 0x41;
assertEquals(ca, ba);
ca = 0x41;
ba = 'A';
assertEquals(ca, ba);
}
}
但是然后:在Java中,只要您处于ascii范围内,char和byte就可以互换,则可以将char常量分配给一个字节,反之亦然。
"string" + char/byte/int
这意味着,您还可以直接将char变量用于循环。但是要当心,构建输出字符串会变得混乱,因为您必须注意哪些东西被“ +”了。您需要将String / char值用作列,将数字用作行。 char + int
...成为字符串。 public void seatnumbersChar() {
int numRows = 3;
int numColumns = 3;
char firstColumnLetter = 'A';
for (int currentRow = 1; currentRow <= numRows; currentRow++) {
for (char currentColumn = firstColumnLetter; currentColumn < firstColumnLetter + numColumns; currentColumn++) {
// at this point you have to watch out currentColumn + currentRow + " " will get evaluated left to right
// currentRow is an int
// currentColumn becomes an int when "+"ed with currentRow
// so currentRow + currentColumn would add two numbers instead of concatenating 2 strings, therefore
// an explicit conversion to string is needed for one of the arguments
System.out.print(currentColumn + String.valueOf(currentRow) + " ");
}
}
}
变成了int?一个字节?一个字符?只是肯定不是String。披露:我试错了字符串串联,它变成一个整数。隐式类型转换的一些示例是here,官方参考是here。
public void seatnumbersByte() {
int numRows = 3;
int numColumns = 3;
byte firstColumnLetter = 'A';
for (int currentRow = 1; currentRow <= numRows; currentRow++) {
for (byte currentColumn = firstColumnLetter; currentColumn < firstColumnLetter + numColumns; currentColumn++) {
// same case other trick: (currentRow + " ") forces the result to be a string due to the + " "
// currentColumn here is declared as byte, when concatenated to a string the numeric representation would be taken (wtf...?)
// therefore a cast to char is needed "(char) currentColumn"
// what's left now ? (currentRow + " ") is a string
// "(char) currentColumn" is a char
// a char "+" a string is a string
System.out.print((char) currentColumn + (currentRow + " "));
}
}
}
同一示例采用字节路由,字符串连接混乱,但不完全相同。
get
希望我在上一次采访中报仇了。反正得到了工作;)
答案 2 :(得分:0)
您需要在内部循环中增加currentColumnLetter。请记住,对于每一行,您需要遍历每一列。那就是你嵌套循环的原因吧?
尝试一下:
for (currentRow = 1; currentRow <= numRows; currentRow++) {
currentColumnLetter = 'A';
for (currentColumn = 1; currentColumn <= numColumns; currentColumn++) {
System.out.print(currentRow);
System.out.print(currentColumnLetter + " ");
currentColumnLetter++
}
}
您不需要在外部循环中使用此条件:
if( currentColumn == numColumns)
{
currentColumnLetter++;
}