Java 如果语句忽略返回方法,其他解决方案是什么?
我有一个名为Cells的类,其中有一个运行以下代码段的更新方法:Java 如果语句忽略返回方法,其他解决方案是什么?,java,arraylist,methods,2d-games,Java,Arraylist,Methods,2d Games,我有一个名为Cells的类,其中有一个运行以下代码段的更新方法: if(goalReached){ if(returnNearestCell() > -1 && isTarget && this.checkCollide(cells.get(returnNearestCell()).x, cells.get(returnNearestCell()).y, cells.get(returnNearestCell()).mass)){ addMass
if(goalReached){
if(returnNearestCell() > -1 && isTarget && this.checkCollide(cells.get(returnNearestCell()).x, cells.get(returnNearestCell()).y, cells.get(returnNearestCell()).mass)){
addMass(cells.get(returnNearestCell()).mass);
cells.get(returnNearestCell()).mass = 20;
cells.get(returnNearestCell()).x = (int) Math.floor(Math.random() * 1001);
cells.get(returnNearestCell()).y = (int) Math.floor(Math.random() * 701);
isTarget = false;
}
if(returnNearestCell() > -1 && !isTarget){
goalX = cells.get(returnNearestCell()).x;
goalY = cells.get(returnNearestCell()).y;
target = cells.indexOf(returnNearestCell());
isTarget = true;
}else if(returnNearestCell() == -1 ){
goalX = (int) Math.floor(Math.random() * 1001);
goalY = (int) Math.floor(Math.random() * 701);
isTarget = false;
}
if(!isTarget){
addMass(5);
}
goalReached = false;
}
总的来说,每个单元格都会寻找最近的质量较小的单元格,如果找到了单元格,则将goalX和goalY设置为该单元格的位置。如果找不到具有相同条件的单元格,则转到随机位置。在由于某种原因第一个if语句被忽略之前,代码工作正常:
returnNearestCell() > -1
然后我得到一个ArrayIndexOutOfBoundsException
我的returnNearestCell方法如下所示:
public int returnNearestCell(){
int x = 0;
int distance = 9999999;
int min = distance;
for(Cell cell : cells){
if(this != cell){
distance = (int)Math.sqrt((this.x - cell.x)*(this.x - cell.x ) + (cell.y - this.y)*(cell.y - this.y));
if(distance < min && this.mass > cell.mass + 10){
min = distance;
x = cells.indexOf(cell);
}else if(distance < min && this.mass < cell.mass + 10 && cell.cellCount == cells.size()){
x = -1;
}
}
}
return x;
}
public int returnNearestCell(){
int x=0;
内部距离=999999;
int min=距离;
用于(单元:单元){
如果(此!=单元格){
距离=(int)Math.sqrt((this.x-cell.x)*(this.x-cell.x)+(cell.y-this.y)*(cell.y-this.y));
if(距离cell.mass+10){
最小值=距离;
x=单元。indexOf(单元);
}否则如果(距离
此方法返回单元格的索引,条件为或-1。我的问题是:有没有办法避免这种越界的感觉?我尝试过多种方法,比如做双重检查,但我仍然遇到同样的问题
cells.get(returnNearestCell()).mass = 20;
cells.get(returnNearestCell()).x = (int) Math.floor(Math.random() * 1001);
cells.get(returnNearestCell()).y = (int) Math.floor(Math.random() * 701);
在这里,您要对单元格进行变异,然后再次调用returnNearestCell()
。由于该方法现在使用更改的参数运行,因此返回值可能不同。最重要的是,您可以沿x
坐标移动单元格,然后在下一次调用returnNearestCell()
进行求值时,它将处于不同的位置
您可能希望查看非原子更新和并发修改,以了解有关此主题的更多信息
有没有办法将对象存储到变量中并通过该变量访问它
是的,这是您问题的解决方案:
if (goalReached) {
// retrieve nearest cell once before modification
final int nearestCellIndex = returnNearestCell();
if (nearestCellIndex > -1 && isTarget) {
// save cell.
final Cell nearestCell = cells.get(nearestCellIndex);
if (this.checkCollide(nearestCell.x, nearestCell.y, nearestCell.mass)) {
// remainder of your code
}
}
}
请注意,最好使用
returnNearestCell()
returnOptional
或至少直接使用Cell
对象。checkCollide()
也是如此,它可以简单地将一个单元格
对象作为参数。我建议您进行适当的诊断工作,以便将其缩小为一个参数。考虑到有一些行为您不理解(假定的“忽略”if语句),我会首先尝试解决这个问题。我还建议在方法中调用一次returnNearestCell
,并在整个方法中使用该结果。你为什么要这么多次地称呼它?我可能会让它返回单元格
(如果没有找到任何内容,则返回null),而不是索引。。。我想这会让你的代码更简单…知道它在哪一行抛出异常会有帮助异常总是在这里抛出:cells.get(returnNearestCell()).y=(int)Math.floor(Math.random()*701);您正在对单元格进行变异,然后再次调用getNearestCell()
,希望结果相同。如果找不到单元格(如果我直接返回单元格),我的方法将返回什么?@BruceTheGoose如果返回类型为cell
,唯一正确的选择是返回null
。如果使用java 8,您可能需要考虑使用和返回<代码>可选。