简体   繁体   中英

properly using constructors to change int value?

I have 2 classes. 1. Main and 2. Gun.

Gun:

public class Gun {
    private int ammoAmount = 15;
    public int getAmmoAmount() { //I believe this allows me to see the value of ammoAmount and use it in Main class.
        return ammoAmount; // Returns the value of ammoAmount to getAmmoAmount?
    }
    public Gun(int ammoUsage) { //this is the constructor right?
        ammoAmount = ammoAmount - ammoUsage; //Method that makes ammoAmount decrease by ammoUsage.
    }
    public void newAmmoAmount() {
        System.out.println("You have " + ammoAmount + " bullet(s) left."); // output of how much bullet is left.
    }
}

Main:

import java.util.Random;
public class Main {

    public static void main(String[] args) {

        Random rand = new Random();
        Gun fire1 = new Gun(0); // I need this to create an objective?
        fire1.newAmmoAmount(); // I need this for code below?
        int clip = fire1.getAmmoAmount(); // I need this to set clip for while loop?
        do { //starts loop
        int x = 5; //max # random can go to.
        int y = rand.nextInt(x); //Makes random integer from 0 to 5 for variable y.
        Gun fire = new Gun(y); //This is the objective that uses the constructor?
        System.out.println("You shot " + y + " bullet(s)."); //Print's out shots from random value y.
        fire.newAmmoAmount(); //uses method in Gun class?
        } while( clip > 0); //loops method till clip is less than 0.
    }
}

I try running the program but it keeps on looping and never ends. The value for ammoAmount is not saved in Gun class. How do I make it so that I can change an int value from a different class?

I had a recent question for it as well. I tried using Constructors as he stated.

How do I call a class into a class from main? and keep the output values?

But as you can see I'm not very successful. This is just a smaller concept of a bigger one I was trying to get to. So basically, did I do Constructors right? And what is the fix for this?

I added some comments to the source code to show you what other questions I might possibly have and if I did those right as well.

Your "Gun" object is immutable - there is nothing that can change state of that object. Such types have a lot of usage, but it is unlikely what you want.

It sounds like you want an instance of "Gun" to "fire" and as result number of renaming bullets in the instance of "Gun" would decrease. Definitely creating new "Gun" object will not change number of bullets in first one.

public class Gun {
    ...
    public fire(int ammoUsage) {
        ammoAmount = ammoAmount - ammoUsage; // TODO: add check for less than 0 
    }
}

With this update class you can fire till no bullets left:

   int maxBulletsPerRound = 5; 
   Gun gun = new Gun(0); // fully loaded
   int clip;
   do {
    int numberOfBullets = rand.nextInt(maxBulletsPerRound); 
    gun.fire(numberOfBullets);
    System.out.println("You shot " + numberOfBullets + " bullet(s)."); 
    gun.newAmmoAmount(); //uses method in Gun class?
    clip = gun.getAmmoAmount(); // check how many bullets left
  } while( clip > 0); 

Note that it may be better to just use while(gun.getAmmoAmount() > 0) .

Ok let me first explain you what you did, and than what I assume you want.

public static void main(String[] args) {
    //create random class instance
    Random rand = new Random();
    //initialize a Gun object with 0 bullets used (so 15 left)
    Gun fire1 = new Gun(0); 
    //the following line just prints "You have 15 bullet(s) left."
    fire1.newAmmoAmount();
    //following line puts 15 in the clip integer
    int clip = fire1.getAmmoAmount(); 
    do { //starts loop
        int x = 5; //max # random can go to.
        int y = rand.nextInt(x); //Makes random integer from 0 to 5 for variable y.
        //you create a new Gun object with y bullets used (you probably did not want that)
        Gun fire = new Gun(y); //This is the objective that uses the constructor?
        System.out.println("You shot " + y + " bullet(s)."); //Print's out shots from random value y.
        //the following line just prints that you have 15-y bullets left
        fire.newAmmoAmount(); //uses method in Gun class?
        //while continues forever, clip is never updated
    } while( clip > 0); //loops method till clip is less than 0.
}

ok I see I am too slow in typing, the answer of @Alexei is probably what you are looking for.

The main point is that you create 2 instances of an object, these two instances cannot affect each other, they are two completely separate things, you can see it as two people (Bas and Alexei) for example, they are both of the class Human (I guess :P), but if you call Bas.setAge(25) the age of Alexei stays unchanged.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM