繁体   English   中英

如何在Java的嵌套for循环中使用字母递增值?

[英]How do I increment a value with letters in a nested for loop in Java?

我是学校的Java初学者学生,并且在我的教科书程序中坚持进行挑战活动,正在使用zybooks.com上的教科书,在此特定活动中,我只能更改程序允许我执行的代码至。 另外,由于我在教科书中所处的位置,我只能使用简单的东西,例如循环和if语句。 这是活动的确切方向,

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

因此,我目前停留的是将列逐渐打印为字母。 现在,当我想要的是1A 1B 1C 1A 2A 2B 2C时,我得到1A 2A 3A,依此类推>如何增加带有字母的列行?

另外,当没有输入列时该程序不应该运行,但是我的程序将打印出行且没有列,这也不是我想要的。 例如,numRows = 3且numColumns = 0,该程序不应打印任何内容,但实际上是该程序仅打印类似123这样的行。如何在没有列的情况下使程序不运行输入?

这是我的代码

 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();

 //what the text book allows me edit
 for(currentRow = 1; currentRow <= numRows; ++currentRow){
    System.out.print(currentRow);
    currentColumnLetter = 'A';

    for(currentColumn = 1; currentColumn <= numColumns; ++currentColumn){
       currentColumn = currentColumnLetter;
       System.out.print(currentColumnLetter + " ");
     }
     ++currentColumnLetter;
 }//the textbook won't let me edit the lines after this brace
  System.out.println("");
 }
}

请记住,我只能在初始化numRows和NumColumns变量之后以及最终的System.out.print(“”);之前)调整代码。

我想你的意思是:

for(currentRow = 1; currentRow <= numRows; ++currentRow){
    for(currentColumn = 0; currentColumn < numColumns; ++currentColumn){
        System.out.print(""+currentRow+(char)('A'+currentColumn) + " ");
    }
}

在Java中, char实际上与int相当相似。 char实际上存储的是一个字符代码,您基本上可以对int进行任何处理。 例如,字符'a'对应于代码97 如果您对(char) 'A' + 1进行了(char) 'A' + 1运算,则结果将为'B' 您可以使用它来增加内部循环中的列字符。

(请注意(char) ,这称为强制转换,它是必需的,以便java知道将结果解释为字符串。在混合类型之间进行操作时,强制转换始终是一个好主意,因此结果是明确的)

ASCII表参考


您的代码在numColumns = 0时打印行,因为在执行第一个print语句之前没有对numColumns进行检查。 您需要确保仅在numColumns > 0时打印输出。

由于currentColumnLetter是一个字符,因此您可以简单地对其进行递增-只需确保将其强制转换即可。 您还需要制作一个新变量,以保持增量不变。
char newLetter = (char) (currentColumnLetter + currentColumn);

特定

numRows = 2
numColumns = 3

for (currentRow = 1; currentRow <= numRows; ++currentRow) {
   currentColumnLetter = 'A';

   for (currentColumn = 1; currentColumn <= numColumns; ++currentColumn) {
      System.out.print(currentRow + String.valueOf(currentColumnLetter++) + " ");
   }
}

那相当于

System.out.print("1" + "A" + " ");
System.out.print("1" + "B" + " ");
System.out.print("1" + "C" + " ");

System.out.print("2" + "A" + " ");
System.out.print("2" + "B" + " ");
System.out.print("2" + "C" + " ");

我想这就是您要瞄准的结果。
注意增量currentColumnLetter++ 这是后递增运算符,它的基本含义是“ 按原样使用变量,然后将其递增 ”。

内部for循环内只能使用1条print语句。
这将打印行号,后跟对应的字符
64 + currentColumn ASCII码,因为A的ASCII码是65:

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int numRows;
    int numColumns;
    int currentRow;
    int currentColumn;

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

    if (numColumns < 1)
        System.exit(1);

    for(currentRow = 1; currentRow <= numRows; currentRow++){
        for(currentColumn = 1; currentColumn <= numColumns; currentColumn++){
            System.out.print(String.valueOf(currentRow) + (char)(64 + currentColumn) + " ");
        }
    }
    System.out.println("");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM