Aloysius Aloysius - 5 months ago 14
Java Question

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? Im 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

Answer

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


    }
Comments