java - 创建生态系统时出现处理错误

标签 java error-handling processing

当我无法让我的动物到达水果时,我正在做一个关于处理的生态系统,起初我试图让动物找到食物然后到达它,但它没有成功,因为动物随机地在屏幕上行走,所以在我的第二次尝试中,我做了一些类似于万有引力的事情,让水果吸引了动物,但我最终得到了这个错误,“ArrayIndexOutOfBoundsException:1”任何人都可以尝试找到这个问题的根源吗?

这是第一页:

Animal[] animal = new Animal[1];

Predator predator;

Fruit[] fruit = new Fruit[2];


void setup() {

  size(1536, 864);


  for(int i = 0; i < animal.length; i++) {

    animal[i] = new Animal();

  }

  predator = new Predator();

  for(int i = 0; i < fruit.length; i++) {

    fruit[i] = new Fruit();

  }

}


void draw() {

  background(60);

  fill(255);

  grid();



  for(int i = 0; i < animal.length; i++) {

    animal[i].display();

    animal[i].update();

    animal[i].checkEdge();

  }  


  for(int i = 0; i < fruit.length; i++) {   

    PVector seek = fruit[i].attractAnimal(animal[i]);

    animal[i].gatherFood(seek);


    fruit[i].display();

  }

}



void grid() {

  strokeWeight(3);

  stroke(65);

  for(int i = 0; i < width; i++) {

    line(70 * i, 0, 70 * i, height);

  }

  for(int i = 0; i < height; i++) {

    line(0, 70 * i, width, 70 * i);

  }

}

这是动物类:

class Animal {

  PVector pos;

  PVector vel;

  PVector acc;

  float mass;


Animal() {

    pos = new PVector(random(0, width), random(0, height));

    vel = new PVector(0, 0);

    acc = new PVector(0, 0);

    mass = random(5, 10);

  }


  void update() {

    pos.add(vel);

    vel.add(acc);

    acc.mult(0);

  }



  void checkEdge() {

    if(pos.x >= width || pos.x <= 0) {

      vel.x = -vel.x;

    }

    if(pos.y >= height || pos.y <= 0) {

      vel.y = -vel.y;

    }

  }  



  void gatherFood(PVector will) {

    PVector w = PVector.div(will, mass);

    acc.add(w);

  }



  void display() {

    fill(255 , 150);

    stroke(100);

    strokeWeight(5);

    ellipse(pos.x, pos.y, mass * 5, mass * 5);

  }

}

这是水果类:

class Fruit {

  PVector pos;


  Fruit() {

    pos = new PVector(random(0, width), random(0, height));

  }



  void display() {

    fill(0, 185, 0);

    stroke(0, 100, 0);

    strokeWeight(3);

    ellipse(pos.x, pos.y, 15, 15); 

  }



  PVector attractAnimal(Animal animal) {

    PVector dir = PVector.sub(pos, animal.pos);

    dir.normalize();

    float walk = 10 / animal.mass;

    dir.mult(walk);

    return dir;

  }


}

最佳答案

您的问题在这里:

for(int i = 0; i < fruit.length; i++) {   

    PVector seek = fruit[i].attractAnimal(animal[i]);
    // this line and the previous for i = 1
    animal[i].gatherFood(seek);


    fruit[i].display();

  }

你有 2 个水果和 1 个动物。当您访问第二个动物 I = 1 的 animal[i] 时,您超出了数组的可用索引,从而导致此异常。

关于java - 创建生态系统时出现处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696732/

相关文章:

Java:注释中注释的目的?

java - 如何在处理中创建对象数组[字面意思]?

java - Hibernate 复合外键失败并生成 ID

java - 从 Java 调用 Oracle "DEFINE"

php - 如何使用 mysqli_multi_query 识别导致错误的查询?

error-handling - 处理请求 future Volley 调用的错误

cakephp - CakePHP cakeError()?

java - 无法从 float 转换为 int

android - 对于Android 3的处理,registerReceiver和getApplicationContext函数不存在

java - 从 Gradle 依赖项中排除 .class 文件