正确使用构造函数来改变int值?

时间:2015-09-21 05:43:38

标签: java constructor integer

我有2节课。 1.主要和2.枪。

枪:

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.
    }
}

主:

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.
    }
}

我尝试运行该程序但它继续循环并且永远不会结束。 ammoAmount的值未保存在Gun类中。我该怎么做才能从不同的类中改变一个int值?

我最近也有一个问题。我按照他的说法尝试使用Constructors。

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

但是你可以看到我不是很成功。这只是我想要达到的更大的一个更小的概念。所以基本上,我做了Constructors吗?对此有什么解决方法?

我在源代码中添加了一些注释,向您展示我可能会遇到的其他问题,以及我是否也做到了这些。

2 个答案:

答案 0 :(得分:1)

你的"枪"对象是不可变的 - 没有什么可以改变该对象的状态。这些类型有很多用处,但它不太可能你想要的。

听起来你想要一个" Gun" to" fire"结果在" Gun"的实例中重命名子弹的数量。会减少。绝对创造新的" Gun"对象不会改变第一个子弹的数量。

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

使用此更新课程,您可以fire直到没有子弹:

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

请注意,最好只使用while(gun.getAmmoAmount() > 0)

答案 1 :(得分:0)

好的,让我先解释一下你做了什么,而不是我想你想要的。

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.
}

好吧,我发现打字速度太慢了,@ Alexei的答案很可能就是你要找的。

重点是你创建一个对象的 2个实例,这两个实例不能相互影响,它们是两个完全不同的东西,你可以把它看作两个人(Bas和Alexei)例如,它们都属于Human类(我猜:P),但如果你打电话给Bas.setAge(25),阿列克谢的年龄保持不变。

希望它有所帮助。