Java 无法使总数减少
我目前正在做这个非常有挑战性的项目,我很难理解。我已经做了很多,但是每次循环后我都很难减少糖果的数量。我怎样才能让每罐糖果都随着总量的减少而减少呢?谢谢你的帮助Java 无法使总数减少,java,if-statement,Java,If Statement,我目前正在做这个非常有挑战性的项目,我很难理解。我已经做了很多,但是每次循环后我都很难减少糖果的数量。我怎样才能让每罐糖果都随着总量的减少而减少呢?谢谢你的帮助 import java.util.Random; public class TreatHouse { int candyPot1; // # of candy in pot 1 int candyPot2; // # of candy in pot 2 int currentPot; // 1 or 2 i
import java.util.Random;
public class TreatHouse {
int candyPot1; // # of candy in pot 1
int candyPot2; // # of candy in pot 2
int currentPot; // 1 or 2
int candyPot;
int totalCandy;
int currentTreaters;
int treatsPerTreater;
public TreatHouse(int candyPot, int totalCandy) {
// ints variable currentPot by parameter candyPot, prints message
if(candyPot !=1 && candyPot !=2) {
//candyPot = 1;
currentPot = 1;
System.out.println("Invalid input, we will use candy pot 1 first.");
}
//ensures total # of candy is more than zero
if(totalCandy <= 0){
this.totalCandy = 0;
System.out.println("We can't give out candy if we don't have any. "
+"\nI think we have some from last year. Yep, we have 100 pieces "
+"\nof candy to give out.");
}else
this.totalCandy = totalCandy;
// splits the candy between the pots
this.totalCandy = this.totalCandy + 100;
candyPot1 = this.totalCandy/2;
candyPot2 = this.totalCandy - candyPot1;
}
public int getCandyCount() {
return candyPot1 + candyPot2;
}
public void passOutCandy() {
/*if there are enough treats per treater for the given amount per treater,
pass out candy from the current pot
else display a messagethat the treaters have been tricked (No candy!)
but don't change the current pot*/
if(currentPot == 1) {
if (treatsPerTreater*currentTreaters <= candyPot1) {
candyPot1 = candyPot1 - (treatsPerTreater*currentTreaters);
} else {
System.out.println("Sorry you've been tricked! No treats for you...");
}
currentPot = 2;
}
else if (currentPot == 2){
if (treatsPerTreater*currentTreaters <= candyPot2) {
candyPot2 = candyPot2 - (treatsPerTreater*currentTreaters);
}
else{
System.out.println("Sorry you've been tricked! No treats for you...");
}
currentPot = 1;
}
}
// Sets the # of trick or treaters
public void knockKnock() {
Random gen = new Random(System.currentTimeMillis());
this.currentTreaters = gen.nextInt(13)+1; // 1 to 13 treaters
}
// Displays how much candy in each pot, total candy left
public void getCandyStatus() {
System.out.println("We have " +this.candyPot1+ " pieces of candy left in pot 1 and " +
this.candyPot2 + " pieces of candy left in pot 2.");
System.out.println("There's a total of " + (this.totalCandy) + " pieces of candy in the two pots.");
}
//returns the pot number for which candy was last given
public int getLastPot() {
return candyPot;
}
public void setTreatsPerTreater(int treatsPerTreater) {
treatsPerTreater = currentTreaters*2;
}
}
提示-去掉这个。totalCandy-在构造函数完成后,你不需要它,所有的糖果都被分成了罐子
将总数保存在一个单独的变量中是不必要的,因为您可以从每个罐中的糖果量计算出来-事实上,以两种方式表示相同的数字(总糖果量)(如
totalCandy
和作为所有罐中糖果的总和)使程序更难正确编写和维护;在你的情况下,这确实是问题的原因。这个建议也被称为。提示-去掉这个。totalCandy-在构造函数完成后,你不需要它,所有的糖果都被分成了罐子
将总数保存在一个单独的变量中是不必要的,因为您可以从每个罐中的糖果量计算出来-事实上,以两种方式表示相同的数字(总糖果量)(如
totalCandy
和作为所有罐中糖果的总和)使程序更难正确编写和维护;在你的情况下,这确实是问题的原因。这个建议也被称为。我怀疑问题就在这里
public void setTreatsPerTreater(int treatsPerTreater) {
treatsPerTreater = currentTreaters*2;
}
这里您没有使用传递的参数
currentTreaters
为0,这将导致TreatsPertCreater
也为0。所以当你调用ourHouse.passOutCandy()时代码>,`pot 1和2的值不会改变。我怀疑问题在这里
public void setTreatsPerTreater(int treatsPerTreater) {
treatsPerTreater = currentTreaters*2;
}
这里您没有使用传递的参数currentTreaters
为0,这将导致TreatsPertCreater
也为0。所以当你调用ourHouse.passOutCandy()时代码>,`pot 1和2的值不会改变。唯一可能出错的地方(据我所知)是这里
在这里,当您修改treatsPerTreater
时,您正在更改局部变量treatsPerTreater
,而不是类变量
也许你是想说
public void setTreatsPerTreater(int treatsPerTreater) {
this.treatsPerTreater = treatsPerTreater;
}
这称为阴影
某些声明可能在其部分范围内被另一个同名声明所掩盖,在这种情况下,不能使用简单名称来引用已声明的实体
有关详细信息,请阅读。
再看一看你唯一可能出错的地方(据我所知)就在这里
在这里,当您修改treatsPerTreater
时,您正在更改局部变量treatsPerTreater
,而不是类变量
也许你是想说
public void setTreatsPerTreater(int treatsPerTreater) {
this.treatsPerTreater = treatsPerTreater;
}
这称为阴影
某些声明可能在其部分范围内被另一个同名声明所掩盖,在这种情况下,不能使用简单名称来引用已声明的实体
有关详细信息,请阅读。
还可以看看总数是多少?答:各部分的总和。你怎么算数?回答:使用+运算符。你想什么时候计算总数?答:这取决于你,但可能在其中一部分发生变化后,我读你的文章越多,我就越不了解你的问题。您有一个getCandyCount()函数来获取总数。您说希望总数减少,但您已经在passOutCandy()函数中减少了currentPot,这导致糖果总数减少。总数是多少?答:各部分的总和。你怎么算数?回答:使用+运算符。你想什么时候计算总数?答:这取决于你,但可能在其中一部分发生变化后,我读你的文章越多,我就越不了解你的问题。您有一个getCandyCount()函数来获取总数。您说希望总数减少,但您已经在passOutCandy()函数中减少了currentPot,这导致糖果总数减少。