在這個程序中,有一個20%
機率變形蟲菌羣如果它有維生素會生病,25%
它會生病,如果它不會。 getSick()
方法是否正確地代表了這一點? 另外,無論我運行程序多少次,殖民地都不會生病,也不會有人死亡。我覺得這很奇特。你有什麼建議我改變?謝謝你的幫助。爲什麼不改變colonySize變量?
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());
}
}
你能解釋getSick函數背後的邏輯嗎?以及它如何獲得維生素價值? –
isVitamin變量在構造函數中傳遞 –