游戏健康计数器

时间:2016-12-07 19:39:14

标签: java counter

我在战舰游戏中遇到健康问题。我需要的是,当调用该方法时,它将需要1次健康。因此,假设健康状态为3,当玩家发货时,该方法会被调用。然后我需要它去health-1,并保持这个价值。然后当health=0时,游戏将结束。

欢迎对此代码提出任何问题和改进,以及批评。

更新:

public static void enemyShoot (int row, int col)
{
    int shot1;
    int shot2;
    int health = 3;

    Random enemyshot = new Random();

    shot1 = enemyshot.nextInt(5)+1;
    shot2 = enemyshot.nextInt(5)+1;

    if (shot1 == row && shot2 == col)
    {
        System.out.println("You Have Been Hit");        
        health = Health(health);
    }               
}

public static int Health (int health)
{       
     if (health == 0){
        System.out.println("You dead");
        System.exit(0);
     }
     health = health-1;
     return health;
}

5 个答案:

答案 0 :(得分:3)

您正在将运行状况初始化为0

int health = 0;

然后当你的射击击中目标时

Health(health);

哪个减去1然后测试为0

health = health-1;
 if (health == 0)

你的健康状况现在为-1所以它永远不会== 0 您应该将运行状况设置为某个正值,或者将测试更改为

if (health <= 0)

答案 1 :(得分:2)

当您将health作为参数传递给Health方法时,它会生成一个要在此方法中使用的副本。因此,当您递减并分析它时,副本会减少到2(如果您最初传递3),但原始变量仍然等于3.因此,实际上,运行状况计数器永远不会递减。

它被称为“按值传递参数”,您可以查看此问题:What's the difference between passing by reference vs. passing by value?

正确的做法是减少方法中的值,然后返回结果。您还应该像这样调用您的方法:

health = Health(health);

另外,arsendavtyan91是对的,你从health = 0开始......你可能想把实际值传递给方法enemyShoot

答案 2 :(得分:1)

  1. 如果health等于零,则无需减去。

  2. 由于您在调用health方法之前始终在int health = 3;方法中enemyShoot()行初始化Health(),因此游戏将继续。< / p>

  3. 所以我建议你在类中声明health(就像一个全局变量)并通过将它传递给constructor来初始化它:

    int health;
    public ClassName(int health){//this is a constructor with the `health` argument
        this.health = health;
    }
    
    public static void enemyShoot (int row, int col)
    {
        int shot1;
        int shot2;
    
        Random enemyshot = new Random();
    
        shot1 = enemyshot.nextInt(5)+1;
        shot2 = enemyshot.nextInt(5)+1;
    
        if (shot1 == row && shot2 == col)
        {
            System.out.println("You Have Been Hit");
    
    
            health = Health(health);
        }
    
    
    }
    
    
     public static int Health (int health)
    {
    
         if (health == 0){
             System.out.println("You dead");
             System.exit(0);
         }
         health = health-1;
         return health;
    }
    

答案 3 :(得分:1)

首先,您应该阅读有关面向对象编程基础知识的更多信息。我的回答是基于您提供的一小段代码,因此我不确定是否已经有适当的实施...

但是,我认为你想要的是创建一个战舰对象,它具有初始健康状态作为成员变量。您可以使用参数定义坐标,方向等,但我会将它们从此示例中删除。在您设法创建此对象后,使用enemyShoot方法计算战舰是否已被击中并降低运行状况。出现这样的事情:

public class Battleship {

    int m_health;

    public BattleShip() {
        m_health = 3;
    }

    public enemyShoot(int x, int y) {
        // TODO: calculate if hit
        if (hit == true) {
            m_health--;
            if (m_health == 0)
                System.out.println("You dead");
            System.exit(0);
        }
    }
}

所以要使用上面的代码,我会把它放在一个名为Battleship.java的文件中,并创建一个main方法来初始化我想要使用的对象

public static void main(String ... args) {
    Battleship bs = new Battleship(); // <-- Creates a new Battleship object
    bs.enemyShoot(0,0); // <-- shoot the battleship
    bs.enemyShoot(0,0);
    bs.enemyShoot(0,0); // <--At this point battleship would be destroyed, if hit
}

您提供的Health方法的问题是将原始数据类型int作为参数(最初设置为0),每次调用Health时都会将其指定为-1。 Callign此方法对enemyShoot方法中的健康值没有影响,因为这不是Object而是原始数据类型的内部方法变量。

希望这可以帮助您继续完成作业。 :)

答案 4 :(得分:1)

你接近你的健康方法,你想要的是这样的:

public static void hit() {
    this.health--;
     if (health <= 0) {
         System.out.println("You died.");
         System.exit(0);
    }
}

Java是一个pass-by-reference,因此您需要更新刚刚命中的对象的运行状况。在这种情况下,我假设您的Health()在该对象内。

否则,您从health = 3;开始,每次点击它都会变为health = 2;,但被点击的对象将始终保持3点状态。再一次,如果不再看到你的代码,我就无法确切地说出最好的方法,所以我不得不假设一些事情。

应该注意的是,这将很快退出程序,您甚至不会看到You died消息。