简体   繁体   中英

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. Also, because of where I am at in the textbook, I can only use simple things like loops and if statements. Here are the exact directions of the activity,

Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 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.

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?

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?

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

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 . What the char actually stores is a character code, which you can do essentially anything you can do to an int with. For example, the character 'a' corresponds to the code 97 . If you did (char) 'A' + 1 , The result would be '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)

ASCII Table Reference


Your code prints the rows when numColumns = 0 because there are no checks on the numColumns before the first print statement executes. You need to make sure that you only print output when numColumns > 0 .

Since currentColumnLetter is a char, you can simply increment it - just make sure to cast it. 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++ . 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.
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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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