我在嘗試在我的Planet類中創建一個布爾方法以按名稱刪除數組並移動值以關閉任何空缺口時出現問題。任何人都可以提供幫助嗎?下面是我的班月球與地球...通過名稱刪除數組並刪除任何空缺的代碼
MOON類: -
public class Moon
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private int orbitalPeriod;
// add constructor here
public Moon(String name, float radius, float distance, float speed, int orbitalPeriod)
{
this.name=name;
this.radius=radius;
this.distance=distance;
this.speed=speed;
this.orbitalPeriod=orbitalPeriod;
}
// add other methods here
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public float getAngle()
{
return angle;
}
public int getOrbitalPeriod()
{
return orbitalPeriod;
}
public void setOrbitalPeriod(int orbitalPeriod)
{
this.orbitalPeriod = orbitalPeriod;
}
public String toString()
{
return "Moon:"+name+" - (orbit= "+orbitalPeriod+")";
}
// This will display the moon when other code is completed.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(149, 149, 149);
ellipse(0, 0, radius*2, radius*2);
popMatrix();
}
}
PLANET類: -
public class Planet
{
private float angle=0.01;
// add class member variables here
private float radius, distance, speed;
private String name;
private Moon [] moons = new Moon [5];
private int numOfMoons = 0;
// add constructor here
public Planet(String name, float radius, float distance, float speed)
{
this.name=name;
this.radius=radius;
this.distance=distance;
this.speed=speed;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public void setRadius(float radius)
{
this.radius = radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public Moon[] getMoons()
{
return moons;
}
public String toString()
{
return "Planet:"+name+" (r= "+radius+ "d= "+distance+") has "+moons.length+" moon(s)";
}
public void printMoons()
{
for (Moon moon : getMoons())
println(moon);
}
public void addMoon(Moon moon)
{
moons[numOfMoons] = moon;
++numOfMoons;
}
public boolean removeMoonByName(String moonName)
{
for (Moon moon : getMoons())
if(moon!=null)
moon.removeMoonByName();
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);
for (Moon moon : getMoons())
if(moon!=null)
moon.display();
popMatrix();
}
}
當你打電話給moon.removeMoonByName()時,你想做什麼?你沒有給你的月球班上一個名字的方法。另外:你是否需要使用數組作爲要求?使用ArrayList可以使你的工作更加簡單,並且不必擔心整個「縮小差距」的問題。 –
看看這裏 - > https://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java#644719 – najjarammar
@ 911DidBush這只是我試圖使該方法的工作。我想要在Array中搜索月球的名稱,然後移除特定的月球,從而消除該過程中的任何空缺。是的,我需要使用一個數組。 –