我不确定问题是否很清楚,也许代码会有所帮助。
这是SpaceGame构造函数的一部分
player.setPlanets(planets); //set Planets for player
enemy.setPlanets(planets); //set Planets for enemy
这是setPlanets方法的一部分
public void setPlanets (ArrayList<Planet> planets) {
for (int i = 0; i < planets.size(); i++) //Iterate through the ArrayList of Planets
{
System.out.println(planets.get(i)); //Prints the info of each planet
}
游戏开始时,行星会打印两次。如果我使用敌人.setPlanets,有没有办法取消它?
最佳答案
您可以使用布尔值作为条件
public void setPlanets (ArrayList<Planet> planets, boolean print) {
for (int i = 0; i < planets.size(); i++) //Iterate through the ArrayList of Planets
{
if (print) {
System.out.println(planets.get(i)); //Prints the info of each planet
}
}
player.setPlanets(planets, true); //set Planets for player
enemy.setPlanets(planets, false); //set Planets for enemy
更多,您可以重载setPlanets
public void setPlanets (ArrayList<Planet> planets) {
setPlanets(planets, false); // don't print by default
}
然后对敌人来说
enemy.setPlanets(planets);
足够。