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

我可以在我做错事情的地方获得帮助吗?我正在创建一个简单的枪支类程序。我的重装方法中出现问题。当我将子弹重新装上枪时,枪中的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

最佳答案

您应按如下所示将其分配回去。

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);


    }

关于java - 返回值不变-Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38215744/

10-12 03:30