我的阵列中发生了奇怪的事情

时间:2015-05-15 21:09:35

标签: java arrays

好的,我要显示我的代码以及我的输入和输出,非常奇怪的是我的数组的值似乎从一行变为下一行。

glBegin(GL_QUADS);

我的意见是:
2
2
2 2
1
6

我的输出是:
这是numIngredients索引:0和值2
这是numIngredients索引:1和值2
numIngredients [0]:2
numIngredients [1]:0
numIngredients的值[k]:0
-1
2
成分:1
  
numIngredients [1]的值从一行变为另一行的2到0,我无法理解最新情况。

1 个答案:

答案 0 :(得分:2)

使用长变量名称即使对于循环变量也很有用 - 您似乎使用i而不是j

for (int j = 0; j < arr.length; j++) // <== possibly arr.length is what you need.
{
    String temp = arr[j]; // <=== was i, same next line
    numIngredients[j] = Integer.parseInt(temp);
    System.out.println(
         "This is numIngredients index: " + j + //<== j this line
         " and value " + numIngredients[j]); // <== again, was using [i] 

}

使用currentIngredient代替j可能有助于找到错误。