返回值不会改变 - java

时间:2016-07-06 02:59:55

标签: java

我通过后,我的参数值似乎没有变化,编辑并返回它 - 它保持不变。

我可以帮助我做错误的地方吗?我正在制作一个简单的枪类课程。我的重装方法出现问题。当我将子弹重新加载到枪中时,枪中的bulletsRemaining成为最大容量,但弹药值不会减少。

public class Gun {

    String name;
    String sound;
    int minDamage;
    int maxDamage;
    int shotsPerSecond;
    int capacity;
    int bulletsRemaining;

    public Gun(String name, String sound, int minDamage, int maxDamage, int shotsPerSecond, int capacity, int bulletsRemaining) {
        this.name = name;
        this.sound = sound;
        this.minDamage = minDamage;
        this.maxDamage = maxDamage;
        this.shotsPerSecond = shotsPerSecond;
        this.capacity = capacity;
        this.bulletsRemaining = bulletsRemaining;
    }

    public void fire() {
        Random rnd = new Random();

        int totalDamage = 0;
        for(int x = 0; x<shotsPerSecond; x++)
        {
            int damage = rnd.nextInt(maxDamage) + 1;
            System.out.print(sound + "(" + damage + ")");
            totalDamage += damage;
        }

        System.out.println(" --> " + totalDamage + " Dmg/Sec ");
    }

    public void fireAtWill() {
        Random rnd = new Random();

        int totalDamage = 0;
        while(bulletsRemaining > 0) {
        for(int x = 0; x<shotsPerSecond; x++)
            {   
                int damage = rnd.nextInt(maxDamage) + 1;
                System.out.print(sound + "(" + damage + ")");
                totalDamage += damage;
                bulletsRemaining--;
                if(bulletsRemaining == 0)
                    break;
            }   

        System.out.println(" --> " + totalDamage + " Dmg/Sec ");
        totalDamage = 0;
        }
    }

    public int reload(int ammo) {
        //System.out.println();
        //System.out.println("Bullets remaining: " + bulletsRemaining);
        //System.out.println("Ammo remaining: " + ammo);
        //System.out.println("Reloading " + name);

        int bulletsReload = capacity - bulletsRemaining; //Amount of bullets to be loaded to gun
        ammo = ammo -= bulletsReload;
        bulletsRemaining = bulletsRemaining += bulletsReload; //Fill bulletsRemaining to the max again after reloading
        return ammo;
    }

    public void supressiveFire(int ammo) {

        Random rnd = new Random();

        int totalDamage = 0;
        while(ammo != 0) {
        while(bulletsRemaining > 0) {
        for(int x = 0; x<shotsPerSecond; x++)
            {   
                int damage = rnd.nextInt(maxDamage) + 1;
                System.out.print(sound + "(" + damage + ")");
                totalDamage += damage;
                bulletsRemaining--;                
            }   

            System.out.println(" --> " + totalDamage + " Dmg/Sec ");
            totalDamage = 0;
            if(bulletsRemaining == 0)
                    reload(ammo);
        }
      }
        if(ammo == 0)
            System.out.println("Out of ammunition)");

    }

    public static void main(String[] args) {
        // TODO code application logic here

        int ammo = 5;
        Gun g1 = new Gun("MK16", "Bang!", 10,15,5,5,5);

        g1.fire();
        g1.reload(ammo);
        System.out.println("Ammo left: " + ammo);
        System.out.println("Bullets left: " + g1.bulletsRemaining);


    }

}

预期输出:

Ammo: 0
Bullets Remaining: 5

我收到的输出:

Ammo: 5
Bullets Remaining: 5

1 个答案:

答案 0 :(得分:2)

您应该将其分配回来,如下所示。

ammo = g1.reload(ammo);

中的

    public static void main(String[] args) {
        // TODO code application logic here

        int ammo = 5;
        Gun g1 = new Gun("MK16", "Bang!", 10,15,5,5,5);

        g1.fire();
        ammo = g1.reload(ammo);
        System.out.println("Ammo left: " + ammo);
        System.out.println("Bullets left: " + g1.bulletsRemaining);


    }