简体   繁体   English

对于循环执行,增加混乱

[英]For loop execution, increment confusion

I don't get it why i in fillArray method ends up being equal to 10 even though the array score is filled only up to index 9 . 我不明白为什么即使数组分数仅填充到index 9 ifillArray方法中最终还是等于10

As per my understanding i has to be smaller than 10 ,so how can it end up being 10 at the end, it should have incremented. 根据我的理解, i必须小于10 ,那么最后它怎么可能变成10 ,它应该增加了。

I tried another loop to test if for loop executes incrementation at the end if condition is true. 我尝试了另一个循环来测试如果条件为true,则for循环是否在最后执行增量。

In test loop i ends up being only 10 which makes sense but the two for loop contradict. 在测试循环中, i最终只有10,这是有道理的,但是两个for循环是矛盾的。

public class GoldScores {
        public static final int MAX_NUMBER_SCORES = 10;
        public static void main(String[] args) {
            double[] score = new double[MAX_NUMBER_SCORES];
            int numberUsed = 0;

            System.out.println("This program reads gold scores and shows");
            System.out.println("how much each differs from the average.");
            System.out.println("Enter gold scores:");
            //numberUsed = fillArray(score);
        //  showdifference(score,numberUsed);
             for(int i=1; i<11; i++){                   //Test loop
                 System.out.println("Count is: " + i);
            }
        }
        private static void showdifference(double[] score, int numberUsed) {
            // TODO Auto-generated method stub

        }
        public static int fillArray(double[] a){
            System.out.println("Enter up to " + a.length + " nonnegative numbers.");
            System.out.println("Mark the end of the list with a negative number.");
            Scanner keyboard = new Scanner(System.in);

            double next = keyboard.nextDouble();
            int i = 0;
            for(i = 0;(next>=0 && i<a.length);i++){     //HELP!!!!
                a[i] = next;
                next = keyboard.nextDouble();
            }
            return i;
        }

You must understand exactly how the for loop works to understand what is going on, and why i is 10 after the for loop in fillArray . 您必须确切了解for循环的工作原理以了解发生了什么,以及为什么fillArray for循环之后i只有10 fillArray

  1. Perform initialization, before the first semicolon. 在第一个分号之前执行初始化。
  2. Test the condition in between the first and second semicolons. 在第一和第二分号之间测试条件。 If the condition is false , break out of the loop. 如果条件为false ,则跳出循环。
  3. Execute the body of the loop. 执行循环的主体。
  4. Perform the statement after the second semicolon (the increment). 在第二个分号(增量)之后执行语句。
  5. Go back to Step 2. 返回步骤2。

In the last iteration of the i for loop, i is 9 , and index 9 is assigned in the array, step 3. Step 4 performs the increment, and i is now 10 . i for循环的最后一次迭代中, i9 ,并在数组中分配索引9 (第3步)。步骤4执行增量,而i现在为10 Then the condition is tested, which is false , and the loop is exited. 然后测试条件,条件为false ,并退出循环。 i is now 10 . i现在10

However, in your main for loop, you print the value in the body instead of examining the looping variable afterwards. 但是,在您的main for循环中,您将在主体中打印该值,而不是随后检查循环变量。 The last iteration is when i is 10 , because the condition is different: i < 11 . 最后一次迭代是当i10 ,因为条件不同: i < 11 If you were to print i after that for loop, you'll see it's 11 . 如果要在for循环之后打印i ,您会看到它是11

in For loop, increment occurs after testing loop condition, not before. 在For循环中,增量发生在测试循环条件之后,而不是之前。 So in last iteration when your condition is checked, I is already equal to 10 and that is exactly what is being returned. 因此,在上一次迭代中,当检查您的条件时,我已经等于10,而这恰恰是返回的结果。 Consider this, if your I would still be 9 in last iteration, your condition would still be true which would mean one more execution in loop. 考虑一下这一点,如果您在上一次迭代中仍为9,则您的条件仍然为true,这意味着需要循环执行一次。

While others have already explained in detail , to eliminate confusion you could modify your code to something like : 尽管其他人已经详细解释了,但是为了消除混乱,您可以将代码修改为:

        double next = keyboard.nextDouble();
        int i = 0;
        int current_i = i;
        for( i = 0; ( next >= 0 && i < a.length ); i++ )
        {
            current_i = i;
            a[i] = next;
            next = keyboard.nextDouble();
        }
        return current_i;

instead of 代替

        double next = keyboard.nextDouble();
        int i = 0;
        for(i = 0;(next>=0 && i<a.length);i++){     //HELP!!!!
            a[i] = next;
            next = keyboard.nextDouble();
        }
        return i;

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

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