简体   繁体   English

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

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

I'm a beginner level Java student in school and I'm stuck on a challenge activity in my textbook program, I'm using a textbook from zybooks.com and in this specific activity I can only change the code that the program allows me to. 我是学校的Java初学者学生,并且在我的教科书程序中坚持进行挑战活动,正在使用zybooks.com上的教科书,在此特定活动中,我只能更改程序允许我执行的代码至。 Also, because of where I am at in the textbook, I can only use simple things like loops and if statements. 另外,由于我在教科书中所处的位置,我只能使用简单的东西,例如循环和if语句。 Here are the exact directions of the activity, 这是活动的确切方向,

Given numRows and numColumns, print a list of all seats in a theater. 给定numRows和numColumns,打印剧院中所有座位的列表。 Rows are numbered, columns lettered, as in 1A or 3E. 行编号,列字母编号,如1A或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 numColumns = 3 prints: 1A 1B 1C 2A 2B 2C. 例如:numRows = 2,numColumns = 3次打印:1A 1B 1C 2A 2B 2C。

So, what I'm currently stuck on is printing the columns incrementally as letters. 因此,我目前停留的是将列逐渐打印为字母。 Right now I'm getting 1A 2A 3A and so on when what I want is 1A 1B 1C 2A 2B 2C> How do I increment that columns row with letters? 现在,当我想要的是1A 1B 1C 1A 2A 2B 2C时,我得到1A 2A 3A,依此类推>如何增加带有字母的列行?

Also, the program shouldn't run when there are no columns input but my program will print out the rows and no columns which isn't what I want either. 另外,当没有输入列时该程序不应该运行,但是我的程序将打印出行且没有列,这也不是我想要的。 For example, numRows = 3 and numColumns = 0, the program shouldn't print anything out but what's happening is that the program will print out only the rows like this 123. How do I get the program to not run if there is no columns input? 例如,numRows = 3且numColumns = 0,该程序不应打印任何内容,但实际上是该程序仅打印类似123这样的行。如何在没有列的情况下使程序不运行输入?

Here is my code 这是我的代码

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

Keep in mind that I can only adjust the code that is after initializing the numRows and NumColumns variables and before the final System.out.print("");. 请记住,我只能在初始化numRows和NumColumns变量之后以及最终的System.out.print(“”);之前)调整代码。

I guess you mean: 我想你的意思是:

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

In Java, char actually works fairly similarity to int . 在Java中, char实际上与int相当相似。 What the char actually stores is a character code, which you can do essentially anything you can do to an int with. char实际上存储的是一个字符代码,您基本上可以对int进行任何处理。 For example, the character 'a' corresponds to the code 97 . 例如,字符'a'对应于代码97 If you did (char) 'A' + 1 , The result would be 'B' . 如果您对(char) 'A' + 1进行了(char) 'A' + 1运算,则结果将为'B' You can use this to increment the column character in your inner loop. 您可以使用它来增加内部循环中的列字符。

(Note the (char) , this is called casting and it is needed so that java knows to interpret the result as a string. It is always a good idea to cast when doing operations between mixed types, so the result is unambiguous) (请注意(char) ,这称为强制转换,它是必需的,以便java知道将结果解释为字符串。在混合类型之间进行操作时,强制转换始终是一个好主意,因此结果是明确的)

ASCII Table Reference ASCII表参考


Your code prints the rows when numColumns = 0 because there are no checks on the numColumns before the first print statement executes. 您的代码在numColumns = 0时打印行,因为在执行第一个print语句之前没有对numColumns进行检查。 You need to make sure that you only print output when numColumns > 0 . 您需要确保仅在numColumns > 0时打印输出。

Since currentColumnLetter is a char, you can simply increment it - just make sure to cast it. 由于currentColumnLetter是一个字符,因此您可以简单地对其进行递增-只需确保将其强制转换即可。 You'll also need to make a new variable to keep your incrementing straight. 您还需要制作一个新变量,以保持增量不变。
char newLetter = (char) (currentColumnLetter + currentColumn);

Given 特定

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++) + " ");
   }
}

That is equivalent to 那相当于

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" + " ");

Which I suppose is the result you're aiming at. 我想这就是您要瞄准的结果。
Note the increment currentColumnLetter++ . 注意增量currentColumnLetter++ This is the post-increment operator, and it basically means " use the variable as is, and after that increment it ". 这是后递增运算符,它的基本含义是“ 按原样使用变量,然后将其递增 ”。

Use only 1 print statement inside the inner for loop. 内部for循环内只能使用1条print语句。
This will print the number of the row followed by the character that corresponds 这将打印行号,后跟对应的字符
to 64 + currentColumn ASCII code, because the ASCII code of A is 65: 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