For loop execution, increment confusion

前端 未结 3 1486
既然无缘
既然无缘 2020-12-22 05:10

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

3条回答
  •  一向
    一向 (楼主)
    2020-12-22 05:43

    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

提交回复
热议问题