简体   繁体   English

嵌套循环输出打印和格式化不正确的表格,这一切都很时髦

[英]Nested for loop output printing and formatting incorrect table, it's all funky

So the goal was to use a nested for loop to output 6 rows and 10 columns.所以目标是使用嵌套的 for 循环来输出 6 行和 10 列。 The thing was though that the inner for loop was supposed to check to see whether the number was even or odd as, if it was even, we would add 2 to it and then print out that number 10 times before moving onto the next output.问题是,内部 for 循环应该检查数字是偶数还是奇数,因为如果是偶数,我们将向其添加 2,然后在移动到下一个输出之前打印该数字 10 次。 So this is what were were supposed to get所以这就是应该得到的

1 1 1 1 1 1 1 1 1 1  
4 4 4 4 4 4 4 4 4 4  
3 3 3 3 3 3 3 3 3 3   
6 6 6 6 6 6 6 6 6 6  
5 5 5 5 5 5 5 5 5 5  
8 8 8 8 8 8 8 8 8 8 

I thought I was on the right track but my output is a complete mess, here's what I have.我以为我在正确的轨道上,但我的输出一团糟,这就是我所拥有的。 Thank you to anyone willing to help.感谢任何愿意提供帮助的人。

for (int numberE = 1; numberE <= 6; numberE++)
{
    for (int nestedE = 1; nestedE < 10; nestedE++)
    {
        if (numberE%2 == 0)
        {
            numberE += 2;
            System.out.printf("%2d", numberE);
        }   else {
              System.out.printf("%2d", numberE);
        }
    }
    System.out.printf("%2d\n", numberE);
}

well to start with your inner loop will only iterate nine times.好吧,从你的内部循环开始只会迭代九次。 second you don't need a nested loop, you need one loop and a guard determining when to print.其次,您不需要嵌套循环,您需要一个循环和一个确定何时打印的守卫。

I would suggest using a temporary variable to store the current intended value.我建议使用临时变量来存储当前的预期值。

The issue with your solution was that you were modifying the value of numberE by using numberE += 2;您的解决方案的问题是您正在使用numberE += 2;修改numberE的值numberE += 2; inside the second for loop, this changes the value globally.在第二个 for 循环中,这会全局更改值。

Moving the final column in to the nested for loops also makes it easier as you wouldn't need to define the temporary variable outside of the loop.将最后一列移到嵌套的 for 循环中也更容易,因为您不需要在循环外定义临时变量。 Using this also meant changing the <10 to <=10 .使用这也意味着将<10更改为<=10

for (int numberE = 1; numberE <= 6; numberE++) {
   for (int nestedE = 1; nestedE <= 10; nestedE++) {
     int current = (numberE % 2 == 0) ? numberE + 2 : numberE;
     System.out.printf("%2d", current);
   }
System.out.printf("\n");
}

You were pretty close though, with practise you'll get better.不过你已经很接近了,通过练习你会变得更好。

Don't modify numberE inside the loops.不要在循环内修改 numberE。 Instead just print numberE + 2.而只是打印 numberE + 2。

Also, if your inner loop runs from 0 to <10 you will get 10 iterations and you don't need to print the number again - just a newline.此外,如果您的内部循环从 0 运行到 <10,您将获得 10 次迭代并且您不需要再次打印数字 - 只是一个换行符。

for (int numberE = 1; numberE <= 6; numberE++)
{
    for (int nestedE = 0; nestedE < 10; nestedE++) // <-- start at 0 and end <10 for 10 iterations
    {
        if (numberE%2 == 0)
        {
            System.out.printf("%2d", numberE + 2); // <-- print the number + 2
        } else {
            System.out.printf("%2d", numberE);
        }
    }
    System.out.println(); // <-- don't print the value again here
}

I would do it this way.我会这样做。 Gives the required result.给出所需的结果。

public class NestedForLoop {

public static void main(String[] args) {
    for (int i = 1; i <= 6; i++)
    {
        int temp = i;
        if(temp%2 == 0) {
            temp +=2;
        }
        for(int j=1;j<=10;j++) {
            System.out.print(temp+" ");
        }
        System.out.println();
    }
}

} }

A brief description of what is happening here: So, since we need 6 rows, we use the value of 6 as a row counter.对这里发生的事情的简要描述:因此,由于我们需要 6 行,因此我们使用 6 的值作为行计数器。 The variable i takes care of keeping a count of the rows.变量 i 负责保持行数。 Here since the target is 6, we start from row number 1 and go until row no 6. Inside each value of the loop, we save the value of i to temp because we don't want the value of i to change before incrementing in the main for loop.这里因为目标是 6,所以我们从第 1 行开始直到第 6 行。在循环的每个值中,我们将 i 的值保存到 temp 因为我们不希望 i 的值在增加之前改变主要的 for 循环。 We then check if this temp value is even by doing a modulo division by 2. If it is even, we increment the temp value by 2.然后我们通过模除以 2 来检查这个临时值是否是偶数。如果是偶数,我们将临时值增加 2。
Then, we run a loop from 1 to 10 since we need 10 columns to print the value temp(either the original i or incremented because it was even).然后,我们运行一个从 1 到 10 的循环,因为我们需要 10 列来打印值 temp(原始 i 或因为它是偶数而递增)。 After exiting the loop, finally to move to the next row, we do a System.out.println().退出循环后,最后移动到下一行,我们执行 System.out.println()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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