2012-06-28 50 views
-4

不工作如果在for循环

 int sum = 0; 
      for (int index = 0; index < ratpop; index++) { 


// verify whether population falls within blast radius 
      if (xcord[index + 1] - xcord[index] <= str 
        || ycord[index + 1] - ycord[index] <= str) 

{ 
       // if it does sum population of both points 
       sum += pop[index] + pop[index + 1]; 
      } 
      } 
      // prints out sum and the cordinates are not correct have to figure out 
      // later 
      System.out.println(xcord[1] + " " + ycord[0] + " " + sum); 
     } 
    } 

我不得不阵列语句列出X cordinates和Y之一的一个cordinates我的逻辑对于这一说法是,如果x cordinate或y cordinate比变量str小于总之,如果不是,但它不会这样做总结它不管。这是我原来的问题,但没有所有额外的东西可能阻止别人给它一个镜头,提前感谢所有使这个网站很好的爱好者,我为误会而道歉。

+4

欢迎访问SO [索引+ 1]。发布家庭作业并要求SO人员调试你的代码在这里被认为是非常糟糕的形式,这解释了downvotes。请阅读[常见问题]和[问]发布准则。 –

+0

作业?至少'index + 1'会给index = ratpop-1一个arrayOutOfBoundsException。我很确定,但这不是你在说的。 –

+0

您可能会发现使用记录器在各个阶段输出信息很有用,并且可能使用IDE来逐步执行代码。最终它必须是你,而不是我们,了解程序的逻辑 –

回答

1

问题是你试图访问索引超出数组边界...

for (int index = 0; index < ratpop - 1; index++) { 

这将让你没有得到一个ArrayIndexOutOfBoundsException