簡體   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