有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要修复for循环

我在上在线课程,这让我很难得到帮助,这就是我来这里的原因。本周的讲座是关于嵌套循环的。他们把我弄糊涂了。我目前被困在这个问题上

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 &燃气轮机

我尝试了许多可能的解决办法,但都产生了许多错误的结果。这是我目前的解决方案

    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

我已经做了两天了,这让我非常沮丧。提前感谢您的帮助


共 (3) 个答案

  1. # 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++;
        }
    }
    
  2. # 2 楼答案

    哟,事情比你想象的还要深刻。我总是觉得这些重复的人物例子很糟糕,我在上次的工作面试中就知道了。现在我们开始:

    如果练习是这样的:从1获取行。。n、 1.的列。。n、 输出应为“1/1,1/2…3/1,3/2…”,这是直截了当的,不是吗

    public void seatnumbersNumbersOnly() {
            int numRows = 3;
            int numColumns = 3;
    
            for (int currentRow = 1; currentRow <= numRows; currentRow++) {
                for (int currentColumn = 1; currentColumn < numColumns; currentColumn++) {
                    System.out.print(currentColumn + "/" + currentRow + " ");
                }
            }
        }
    

    但任务是不同的,他们希望列中有字母。让我们在同样的基础上对其进行暴力

    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
        }
    }
    

    }

    但是:在Java中,字符和字节是可互换的,只要您在ascii范围内,就可以将字符文本分配给字节,反之亦然

    @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);
    }
    

    这意味着,您还可以直接使用char变量进行循环。但是请注意,构建输出字符串会变得很混乱,因为您必须观察哪些内容会被“+”加上哪些内容。您需要列的字符串/字符值和行的数字。A "string" + char/byte/int。。。变成一个字符串char + int变成和int?一个字节?炭?当然不是一根绳子。披露:我尝试了字符串连接并出错,它变成了一个int。隐式类型转换的一些示例是here,官方引用是here

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

    同样的例子是字节路由,类似的字符串连接混乱,但不完全相同

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

    希望我能在上次面试中报仇。不管怎样,我还是得到了这份工作;)

  3. # 3 楼答案

    您需要在内部循环中增加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++;
    }