【发布时间】: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