简体   繁体   English

由于某种原因,循环不会打印出正确的数组值

[英]For some reason loop doesn't print out correct array values

I'm having some trouble getting the correct output when printing an array. 打印阵列时,我在获取正确输出时遇到一些麻烦。 Essentially what I'm trying to do is set up an array in the main method, then send that array to another method that would print out something like this: 基本上我要做的是在main方法中设置一个数组,然后将该数组发送到另一个打印出这样的东西的方法:

   89   12   33   7   72   42   76   49
   69   85   61   23

With it being 3 spaces to the right and starting a new print line after the 8th number. 右边是3个空格,在第8个数字后面开始新的打印行。 Seems easy enough but I get something like this instead. 看起来很简单,但我得到的是这样的东西。

   89
   69   85   61   23

It doesn't print out the values between position 1 and 7 for some reason. 由于某种原因,它不会打印位置1和7之间的值。 This is what I have. 这就是我所拥有的。

public class Test
{
    public static void main (String [] args)
    {
        int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
        printArrayValues(myInches);
    }

    public static void printArrayValues(int [] myInchesParam) {
        for (int i = 0; i < 8; i++) {
            System.out.print("   " + myInchesParam[i]);
            System.out.println();
            for (i = 8; i < 12; i++) {
                System.out.print("   " + myInchesParam[i]);
            }
        }
    }
}

Should I use a do-while instead? 我应该使用do-while吗? Or can I still do it with a for loop and I'm just doing it wrong? 或者我仍然可以用for循环来做它我只是做错了吗?

There are many ways to solve this but one would be using the modulo operator to check if 8 entries have already been printed. 有很多方法可以解决这个问题,但有一种方法可以使用模运算符来检查是否已经打印了8个条目。 You add 1 to i because your array is 0 indexed. 您向i添加1,因为您的数组是0索引。

   for (int i = 0; i < myInchesParam.length; i++) {      
        System.out.print(myInchesParam[i] + "   ");
        if((i + 1) % 8 == 0) {
            System.out.println();
        }
   }

EDIT : The benefit of this method is that it works for any array length. 编辑 :此方法的好处是它适用于任何数组长度。 Some of the other suggestions will not. 其他一些建议则不会。

Well, what is happening is that in the loop i starts as 0, then when it reaches the second loop you set i to 8, and thus the condition i < 8 is no longer valid. 好吧,发生的事情是在循环中我从0开始,然后当它到达第二个循环时你将i设置为8,因此条件i <8不再有效。 Easiest fix would be to not nest your loops but have 最简单的修复方法是不嵌套你的循环,但有

    for (int i = 0; i < 8; i++) {
        System.out.print("   " + myInchesParam[i]);
    }
    System.out.println();
    for (i = 8; i < 12; i++) {
            System.out.print("   " + myInchesParam[i]);
    }

instead. 代替。 Even nicer is probably 甚至可能更好

    for (int i = 0; i < 12; i++) {
        System.out.print("   " + myInchesParam[i]);
        if(i==7) {
          System.out.println();
        }
    }

The problem is you that you are using the same variable in two nested for loops. 问题是您在两个嵌套for循环中使用相同的变量。 This will cause the outer array to stop after the first iteration and print only the values in the second line. 这将导致外部数组在第一次迭代后停止,并仅打印第二行中的值。

Just use one loop and print a new line if i > 0 && i % 8 == 0 : 如果i > 0 && i % 8 == 0只需使用一个循环并打印一个新行:

public static void printArrayValues(int[] myInchesParam) {
    for (int i = 0; i < myInchesParam.length; i++) {
        if (i > 0 && i % 8 == 0)
            System.out.println();
        System.out.print("   " + myInchesParam[i]);
    }
}

Alternatively you can just use i % 8 === 7 to insert a new line afterwards: 或者你可以使用i % 8 === 7然后插入一个新行:

public static void printArrayValues(int[] myInchesParam) {
    for (int i = 0; i < myInchesParam.length; i++) {
        System.out.print("   " + myInchesParam[i]);
        if (i % 8 == 7)
            System.out.println();
    }
}

But you could have a trailing new line in some cases with this last solution. 但是在某些情况下,您可以使用最后一个解决方案获得一个尾随的新行。

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

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