gpt4 book ai didi

java - 为什么菌落大小变量没有改变?

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:58 24 4
gpt4 key购买 nike

在这个程序中,如果阿米巴虫群有维生素,就有 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());


}

}

最佳答案

某件事发生的概率为 20%,这意味着五分之一的概率应该是正确的。但实际上您计算出的患病率为 19 次中有 1 次(nextInt(19) 为您提供 0 到 18 之间的数字,随机)。

因此,对于 20% 的可能性,您需要使用 n.getInt(5),对于 25% 的可能性,您需要使用 n.getInt(4)

除此之外,只需运行足够的次数即可。我不认为你运行它的次数不够,因为你的程序每次运行它时都会通过几个对话框来运行它。但我运行它并得到了几个“did”。

关于java - 为什么菌落大小变量没有改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com