我的问题是,在我的代码中,if语句应该从某个底池获取然后返回值,而是从两个底池而不是一个中获取值,无法弄清楚如何修复它。另外我的另一个问题是,每次从一个罐中取出糖果,它应该切换到另一个锅,我该怎么办呢?我正在谈论的if语句是在PassOutCandy类中。
import java.util.Random;
public class TreatHouse {
int candyPot1; // amount of candy in pot 1
int candyPot2; // amount of candy in pot 2
int currentPot; // 1 or 2
int totalCandy;
int currentTreaters; // variables
int treatsPerTreater;
public TreatHouse(int candyPot, int totalCandy) {
// Add code here, be sure to split the candy between the pots.
currentPot = candyPot;
this.totalCandy = totalCandy;
if (totalCandy <=0) {
System.out.println("We can't give out candy if we don't have amy. I think we have some from last year. " +
"Yep, we have 100 pieces of candy to give out.");
totalCandy = 100;
candyPot1 = totalCandy/2; // splits candy between both pots and sets totalCandy to 100 if user puts in false input
candyPot2 = totalCandy/2;
}
if (totalCandy > 0) {
this.totalCandy = totalCandy;
candyPot1 = totalCandy/2; // with correct user input splits the candy properly between both pots
candyPot2 = totalCandy - candyPot1;
}
}
public int getCandyCount() {
return candyPot1 + candyPot2; // total candy left
}
public void passOutCandy() {
//If there are enough treats per treater for the given amount per treater, pass out candy
//from the current pot and switch to the other one.
//Else display a message that the treaters have been tricked... (no candy for them.)
// but do not change the current pot
if ((totalCandy > 0) && (treatsPerTreater < totalCandy)) {
if (currentPot == 1 || currentPot == 2) {
if (currentPot == 1) {
candyPot1 = candyPot1 - (this.currentTreaters*this.treatsPerTreater);
}
if (currentPot == 2) {
candyPot2 = candyPot2 - (this.currentTreaters*this.treatsPerTreater);
}
}
else
System.out.println("Pot " + currentPot + " doesn't exist. Try again next time");
}
else
System.out.println("You have been tricked! No candy for you >:D!!!!");
}
//Sets the number 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() {
//add in code here
System.out.println("Candy in Pot1: " + candyPot1 + " Candy in Pot2: " + candyPot2); // will display candy in pot 1 / pot 2
System.out.println("Total candy left: " + (candyPot1 + candyPot2)); // total candy left
}
//returns the pot number for which candy was last given.
public int getLastPot() {
if (currentPot == 1 || currentPot == 2) {
if (currentPot == 2) { // returns the last pot that candy was taken from
currentPot = 1;
return currentPot;
}
if (currentPot == 1) {
currentPot = 2;
return currentPot;
}
}
else
return currentPot;
}
public void setTreatsPerTreater(int treatsPerTreater) {
//add code here
this.treatsPerTreater = treatsPerTreater; // sets the proper amount of treats per treater from user input
}
}