2015-10-06 203 views
1

在這個程序中,有一個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()); 


    } 

} 
+0

你能解釋getSick函數背後的邏輯嗎?以及它如何獲得維生素價值? –

+0

isVitamin變量在構造函數中傳遞 –

回答

0

20%的可能性表示5分之1的應該是真的。但是實際上,您在19次計算疾病爲1(nextInt(19)隨機給出您包含0到18之間的數字)。

因此,對於20%,您需要使用n.getInt(5)並有25%的機會需要使用n.getInt(4)

除此之外,只要運行足夠多的時間。假設您的程序每次運行時都會通過多個對話框運行,我認爲您沒有足夠的時間運行足夠的次數。但我跑了它,並得到了幾個「做」。

+0

謝謝你爲我清理那個,它總是讓我困惑。 –

相關問題