【问题标题】:Displaying things in processing显示处理中的东西
【发布时间】:2017-03-16 23:44:37
【问题描述】:

我正在尝试编写一个程序来绘制 100 个具有不重叠的随机 x 和 y 坐标的椭圆。

我编写了这段代码,但由于某种原因,当我运行它时没有显示任何内容。它不会抛出任何错误或任何东西,但它只是不起作用。

static person[] population;

void setup()  {
  population=new person[100];
  size(1000,700);
  stroke(0);

  createPopulation(50);
  for(int i=0; i<100; i++){
    fill(population[i].getR(), population[i].getG(), population[i].getB());
    ellipse(population[i].getX(), population[i].getY(), 50, 50);
  }

}

void draw()  {
  for(int i=0; i<100; i++){
    fill(population[i].getR(), population[i].getG(), population[i].getB());
    ellipse(population[i].getX(), population[i].getY(), 50, 50);
  }
}


class person {
  int x,y;
  boolean immune;
  boolean sick;
  int r;
  int g;
  int b;

  person(int xPos, int yPos, boolean isImmune, boolean isSick){
    x=xPos;
    y=yPos;
    immune=isImmune;
    sick=isSick;
  }  

  void checkIfSick(){
    if(sick){
      r=255;
      g=0;
      b=0;
    }
    else{
      r=100;
      g=100;
      b=255;
    }
  }

  int getR(){
    return this.r;
  }
  int getG(){
    return this.g;
  }
  int getB(){
    return this.b;
  }
  int getX(){
    return this.x;
  }
  int getY(){
    return this.y;
  }
}

void createPopulation(int percentImmune){
  int[] xPositions=new int[100];
  int[] yPositions=new int[100];
  int i=0;
  while(i<percentImmune){
    boolean xMatch=false;
    boolean yMatch=false;
    int randX=(int)Math.round(Math.random()*1000);
    int randY=(int)Math.round(Math.random()*700);

    population[i]=new person(randX, randY, true, false);

    for(int n=0; n<i; n++){
      if(randX/25==xPositions[n]){
        xMatch=true;
      }
      if(randY/25==yPositions[n]){
        yMatch=true;
      }
    }

    if(xMatch==false || yMatch==false){
      xPositions[i]=randX/25;
      yPositions[i]=randY/25;
      i++;
    }
  }
  while(i<100){
    boolean xMatch=false;
    boolean yMatch=false;
    int randX=(int)Math.round(Math.random()*1000);
    int randY=(int)Math.round(Math.random()*700);

    population[i]=new person(randX, randY, false, false);

    for(int n=0; n<i; n++){
      if(randX/25==xPositions[n]){
        xMatch=true;
      }
      if(randY/25==yPositions[n]){
        yMatch=true;
      }
    }

    if(xMatch==false || yMatch==false){
      xPositions[i]=randX/25;
      yPositions[i]=randY/25;
      i++;
    }
  }
}

【问题讨论】:

  • 很遗憾,SO不提供调试服务。
  • @WasiAhmad 对不起,如果它像我要求你调试我的代码一样脱落,但这是我第一次使用处理,所以我只是希望有人让我知道什么我在这里做错了。我很确定这与变量的范围有关,但我对这些东西还是很陌生,所以如果你能告诉我是否是这种情况,我将不胜感激。
  • 这段代码是怎么调用的?
  • @ScaryWombat 据我了解,在处理过程中它只运行一次设置,然后循环绘制永远。您也不必显式调用。

标签: java function scope processing


【解决方案1】:

您需要养成调试程序的习惯。无耻的自吹自擂:我写了一篇调试Processing sketches的教程here

我的猜测是,您的代码卡在了您的 createPopulation() 函数中的一个 while 循环中。调试会告诉你是哪一个,然后你就可以修复你的错误了。

你也应该养成breaking your problem down into smaller steps的习惯。您不应该编写整个程序然后想知道它为什么不工作。取而代之的是,一次只测试一件事的小块工作。然后,当您遇到错误时,您可以发布 MCVE 而不是整个草图。

【讨论】:

  • 是的,在createPopulation 中,i 的值不会在每种情况下都增加,可能会导致无限循环
  • @ScaryWombat 对。但真正的教训是 OP 需要学习如何调试他们的程序。
  • OP,另见talk to the duck
  • 非常感谢!我读了你的教程,我将来会这样做。
【解决方案2】:

使用“random()”在随机位置显示人物

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多