正确使用构造函数来更改 int 值?
properly using constructors to change int value?
我有 2 个 class。 1. Main 和 2. 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.
}
}
主要:
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 class 中。我该怎么做才能从不同的 class?
更改 int 值
我最近也有一个问题。我试过按照他所说的那样使用构造函数。
How do I call a class into a class from main? and keep the output values?
但是如您所见,我不是很成功。这只是我试图达到的更大概念的一个更小概念。所以基本上,我做的构造函数对吗?解决这个问题的方法是什么?
我在源代码中添加了一些注释,以向您展示我可能遇到的其他问题以及我是否也做对了。
您的 "Gun" 对象是不可变的 - 没有任何东西可以改变该对象的状态。这样的类型有很多用法,但不太可能是你想要的。
听起来您想要 "Gun" 的实例到 "fire",因此 "Gun" 实例中重命名项目符号的数量会减少。绝对创建新的 "Gun" 对象不会改变第一个中的项目符号数。
public class Gun {
...
public fire(int ammoUsage) {
ammoAmount = ammoAmount - ammoUsage; // TODO: add check for less than 0
}
}
通过这次更新 class 你可以 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)
。
好吧,让我先解释一下你做了什么,而不是我假设你想要的。
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)例如,他们都是 class Human
(我猜 :P),但是如果你调用 Bas.setAge(25)
,Alexei 的年龄保持不变。
希望对您有所帮助。
我有 2 个 class。 1. Main 和 2. 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.
}
}
主要:
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 class 中。我该怎么做才能从不同的 class?
更改 int 值我最近也有一个问题。我试过按照他所说的那样使用构造函数。
How do I call a class into a class from main? and keep the output values?
但是如您所见,我不是很成功。这只是我试图达到的更大概念的一个更小概念。所以基本上,我做的构造函数对吗?解决这个问题的方法是什么?
我在源代码中添加了一些注释,以向您展示我可能遇到的其他问题以及我是否也做对了。
您的 "Gun" 对象是不可变的 - 没有任何东西可以改变该对象的状态。这样的类型有很多用法,但不太可能是你想要的。
听起来您想要 "Gun" 的实例到 "fire",因此 "Gun" 实例中重命名项目符号的数量会减少。绝对创建新的 "Gun" 对象不会改变第一个中的项目符号数。
public class Gun {
...
public fire(int ammoUsage) {
ammoAmount = ammoAmount - ammoUsage; // TODO: add check for less than 0
}
}
通过这次更新 class 你可以 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)
。
好吧,让我先解释一下你做了什么,而不是我假设你想要的。
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)例如,他们都是 class Human
(我猜 :P),但是如果你调用 Bas.setAge(25)
,Alexei 的年龄保持不变。
希望对您有所帮助。