在这个程序中,如果变形虫有维生素,那么变形虫会有20%
个病,25%
如果没有,它会生病。 getSick()
方法是否正确表示了这一点?
此外,无论我多少次运行该程序,殖民地都不会生病,并且没有一个死亡。我发现它非常奇特。你有什么建议我改变?谢谢你的帮助。
public class AmoebaColony {
//instance variables
private String name;
private String caretakerName;
private long colonySize;
private boolean isVitamin;
private int successfulBreeds = 0;
private boolean sickness;
//AmoebaColony constructor
public AmoebaColony(String name, String caretakerName, long colonySize,
boolean isVitamin) {
this.name = name;
this.caretakerName = caretakerName;
this.colonySize = colonySize;
this.isVitamin = isVitamin;
}
public void feed()
{
colonySize *= 2;
successfulBreeds++;
}
public void getSick()
{
Random n = new Random();
if (isVitamin)
{
int c1 = n.nextInt(19);
if (c1 == 0)
{ sickness = true; }
else
{ sickness = false; }
}
else if (isVitamin == false)
{
int c2 = n.nextInt(24);
if (c2 == 0)
{ sickness = true; }
else
{ sickness = false; }
}
}
public String isSick()
{
if (sickness == true)
{
return "did";
}
else
{
return " did not ";
}
}
public void die() {
if (sickness == true)
{
colonySize = colonySize - (colonySize/10);
}
else if (sickness == false)
colonySize = colonySize;
}
//returns how many times it successfully bred
public int getSuccess()
{
return successfulBreeds;
}
//returns size of colony
public long getSize()
{
return colonySize;
}
}
public class AmoebaColonyTester {
public static void main(String[] args) {
//Get variables
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?");
long colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "Welcome, " + caretakerName + "! 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 feedCounter = 0;
//create colony object
AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, isVitamin);
//feed the colony so it can breed
for(int x = 1; x <= feedTimes; x++)
{
if (x <= breedTimes)
{
amoeba.feed();
feedCounter++;
}
}
//get maximum colony size
long finalColony = amoeba.getSize();
;
//amoeba get sick :/
amoeba.getSick();
amoeba.die();
System.out.println(amoeba.getSize());
//display info on the screen
JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
"\nThe starting colony size was " + colonySize + "\nThey were fed " + feedCounter
+ " times \nRequested number of times to breed: " + breedTimes
+ " \nThey successfully bred " + amoeba.getSuccess() + " times\nThe colony " + amoeba.isSick() +
" get sick and "+ (finalColony - amoeba.getSize()) + " died \n"
+ " The final size of the colony is " + amoeba.getSize());
}
}
答案 0 :(得分:0)
20%的可能性意味着5次中的1次应该是真的。但你实际上将疾病计算为19次中的1次(nextInt(19)
为你提供0到18之间的数字,随机)。
因此,对于20%,您需要使用n.getInt(5)
,并且有25%的可能性需要使用n.getInt(4)
。
除此之外,只需运行足够的时间。考虑到你的程序每次运行时都会运行几个对话框,我认为你没有足够的时间运行它,只有19%的机会。但我跑了它并得到了几个&#34;做了#34;。