需要修复循环 [英] Need to fix for loop

查看:61
本文介绍了需要修复循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上在线课程,这使得寻求帮助变得更加困难,这就是为什么我在这里.本周的讲座是关于嵌套循环的.他们把我弄糊涂了.我目前正陷在这个问题上.

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

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

我已经做了两天了,这让我感到非常沮丧.预先感谢您的帮助.

解决方案

您非常接近.

但是,您没有正确处理列名.在每一行开始时,您需要返回到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++;
    }
}

I'm taking online classes, which makes it harder to get help, which is why I'm here. This week's lecture is on nested-loops. They are confusing the heck out of me. I am currently stuck on this problem.

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 >

I have tried many possible solutions which have yield many wrong results. This is my current solution

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

The code yields this result

1A 1A 2B 2B 

The desired result is

1A 1B 2A 2B

I've been going at it for two days and its making me seriously frustrated. Thanks in advance for any help.

解决方案

You're pretty close.

However, you're not handling the column name correctly. As each row starts, you need to go back to A, and increment by one with each column:

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

It's also pretty weird to be using 1-based indexing. Indexing in Java (as well as many other languages) should be started at 0. Your column loop doesn't perform enough iterations as a result - if you set numColumns to 2, you'll only print a single column.

It would be more idiomatic to write the loops like this:

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

这篇关于需要修复循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆