简体   繁体   English

如果使用特定参数调用方法,如何取消打印语句?

[英]How to cancel a print statement if a method is called with a certain parameter?

I'm not sure if the question is very clear, maybe the code will help. 我不确定问题是否很清楚,也许代码会有所帮助。

This is part of the constructor of SpaceGame 这是SpaceGame构造函数的一部分

   player.setPlanets(planets); //set Planets for player

   enemy.setPlanets(planets); //set Planets for enemy

And this is part of the setPlanets method 这是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
        }

When the game begins, planets are printed twice. 游戏开始时,行星会打印两次。 Is there a way to cancel it if I use enemy.setPlanets? 如果我使用敌人.setPlanets,是否可以取消它?

You can use a boolean as a condition 您可以使用布尔值作为条件

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

And more you can overload setPlanets 更多,您可以重载setPlanets

public void setPlanets (ArrayList<Planet> planets) {
    setPlanets(planets, false); // don't print by default
}

Then for enemy, 然后对敌人来说

enemy.setPlanets(planets);

is enough. 足够。

I see there's two different info you will print. 我看到您将打印两种不同的信息。 once for player and another for enemy so where is the problem it's different info, so if you want to print just one info when start the game you should write: 一次是给玩家,另一次是给敌人,所以问题出在哪里,那就是不同的信息,因此,如果您想在开始游戏时仅打印一个信息,则应编写:

if (gameStarted){
     player.setPlanets(planets); //set Planets for player        
}else{
     player.setPlanets(planets); //set Planets for player        
     enemy.setPlanets(planets); //set Planets for enemy
}

or viceversa. 或相反亦然。

I think bejond's answer is quite good, but it will check the print condition for each element of your loop. 我认为bejond的答案是相当不错的,但是它将检查循环中每个元素的打印条件。 You could check the condition before entering the loop to obtain better performances. 您可以在进入循环之前检查条件以获得更好的性能。

player.setPlanets(planets, true); //set Planets for player
enemy.setPlanets(planets, false); //set Planets for enemy

Your function should now look like that 您的函数现在应如下所示

public void setPlanets (ArrayList<Planet> planets, boolean isPrinted) 
{

        if (isPrinted) 
        {
           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
           }
        }

        //your code...
}

If you wish, you could also overload your function, like bejond suggested. 如果愿意,也可以重载函数,如bejond建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM