Aloysius
Aloysius

Reputation: 29

Return value does not change - java

My parameter value doesn't seem to change after I pass it through, edit it and return it - It remains the same.

Can I have some help on where I am doing wrongly? I'm creating a simple gun class program. The problem occurs in my reload method. When I reload the bullets to the gun, the bulletsRemaining in the gun becomes the maximum capacity, but the ammo value doesn't decrease.

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


    }

}

Expected output:

Ammo: 0
Bullets Remaining: 5

Output I received:

Ammo: 5
Bullets Remaining: 5

Upvotes: 1

Views: 91

Answers (1)

Beniton Fernando
Beniton Fernando

Reputation: 1533

You should assign it back like below.

ammo = g1.reload(ammo);

in the main

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


    }

Upvotes: 2

Related Questions