[英]Why doesn't the method breed return an int?
When the breed method is called and there is food available, the size of the colony doubles. 当使用繁殖方法并且有食物可用时,菌落的大小就会增加一倍。 The user is asked how many times they want to feed the colony and how many times they want the colony to breed.
询问用户要饲养菌落多少次以及要繁殖菌落多少次。 I need to output the amount of times that it successfully bred.
我需要输出成功繁殖的次数。 I created an integer called success to keep track of the times it successfully bred, but (not surprisingly for me) does not work and returns 0. How can I fix this problem?
我创建了一个称为成功的整数来跟踪其成功繁殖的时间,但是(对我来说并不奇怪)它不起作用并返回0。如何解决此问题? Thanks.
谢谢。
AmoebaColony tester class 变形虫测试仪类
import javax.swing.JOptionPane;
public class AmoebaColonyTester {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null, "What will be the name of the colony?");
String caretakerName = JOptionPane.showInputDialog(null, "What is the name of the caretaker of the colony?");
int colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the starting size of the colony?"));
int feedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want to feed the colony?"));
int breedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want your colony to breed?"));
int vitamin = JOptionPane.showConfirmDialog(null,"Do you want to give vitamins to your colony?", "Please select",
JOptionPane.YES_NO_OPTION);
boolean isVitamin;
if (vitamin == 1)
isVitamin = true;
else
isVitamin = false;
int success = 0;
AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, feedTimes,
breedTimes, isVitamin);
for(int x = 1; x <= breedTimes; x++)
{
success += amoeba.breed();
}
amoeba.howManyDead();
System.out.println("the size of the colony is " + amoeba.getSize());
JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
"\nThe starting colony size was " + colonySize + "\nThey were fed " + feedTimes
+ " times \nRequested number of times to breed: " + breedTimes
+ "\nThey successfully bred " + success + "times \nThe final size of the"
+ " colony is " + amoeba.getSize());
}
}
AmoebaColony 变形虫
public class AmoebaColony {
private String name;
private String caretakerName;
private int colonySize;
private int feedTimes;
private int breedTimes;
boolean isVitamin;
int successfulBreeds;
public AmoebaColony(String name, String caretakerName, int colonySize,
int feedTimes, int breedTimes, boolean isVitamin) {
this.name = name;
this.caretakerName = caretakerName;
this.colonySize = colonySize;
this.feedTimes = feedTimes;
this.breedTimes = breedTimes;
this.isVitamin = isVitamin;
}
public int getSize()
{
return colonySize;
}
public int breed()
{
if(breedTimes <= feedTimes){
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
}
else{
feedTimes--;
breedTimes--;
return 0;
}
}
public int howManyDead()
{
Random random = new Random();
int pop = colonySize;
if (isVitamin)
{
int c1 = random.nextInt(4);
if (c1 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
else if (isVitamin == false)
{
int c2 = 1 + random.nextInt(3);
if (c2 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
return pop - colonySize;
}
}
Sample input: Colony name : bobo Caretaker name : Peter colony size : 500 feed times: 4 breed times: 7 // in this case it will breed successfully 4 times isVitamin: No vitamins (false) 样本输入:菌落名称:bobo看守者名称:Peter菌落大小:500饲料次数:4繁殖次数:7 //在这种情况下,它将成功繁殖4倍is维生素:无维生素(假)
I would recommend just running this through some simple debugging. 我建议仅通过一些简单的调试来运行它。 Follow these steps and it should narrow down where your problem is.
请按照下列步骤,它应该缩小您的问题所在。
From what I can see of the code, I do not see a reason it should not properly update the success. 从我所看到的代码中,我看不到它不应该正确更新成功的原因。
Edit I see your problem. 编辑我看到你的问题。 You are inputting the values specified, and this is how the program interprets it.
您正在输入指定的值,这就是程序对其进行解释的方式。 Your breed function uses the following values, you enter breed=7, feed=4.
您的品种函数使用以下值,输入品种= 7,提要= 4。 I put these values into the function below...
我将这些值放入下面的函数中...
if(7<= 4){ // this is obviously false, so it uses the else
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
} else{
//Since this is run, and both are decremented togeather, you will always be in the Else.
feedTimes--;
breedTimes--;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.