2014-10-07 72 views
-2

我對編碼頗爲陌生,而且遇到了一些問題。初學者的Java Help IF/THEN語句?

我需要創建一個程序,允許用戶創建他們自己的阿米巴殖民地。其中一個要求是包括給他們維生素和疾病。

維生素 - 維生素可以幫助我們的阿米巴保持健康。詢問你的用戶是否想給他們的殖民地維生素。

疾病 - 不幸的是,所有的寵物有時會生病。當發生這種情況時,10%的阿米巴人死亡。每場會議:

如果殖民地沒有給予維生素,它有25%的機會,它可能會生病。

如果菌落含有維生素,它有20%的機會可能會生病。

我用一個隨機數發生器來得到疾病的百分比,但由於某種原因,我的if/then語句有一些wrog,因爲它從不使得疾病等於true,它總是使它等於false,我有它設置爲布爾值,並聲明一些if/then語句使疾病等於false。

編輯:我列出了下面的代碼,而不是鏈接,我添加了luisluix提供的代碼,現在似乎工作。謝謝!

我的代碼如下所示:

import java.lang.Math; 
import java.util.Random; 
import javax.swing.JOptionPane; 

public class AmoebaColony { 

private String colonyName; 
private String caretakerName;; 
private String isItSick; 
private int daysFed; 
private int breedRequest; 
private int breedSuccess; 
private int rNumber; 
private int requestVitamins; 
private double startingSize; 
private double colonySize; 
private double finalSize; 
private double died; 
private boolean sickness; 
private boolean vitamin; 

public void setColonyProperties() 
{ 
    JOptionPane.showMessageDialog(null, "Welcome new caretaker, this program allows you to create, manage, and develope your own amoeba colony.", 
    "Welcome!", JOptionPane.INFORMATION_MESSAGE); 
    colonyName = JOptionPane.showInputDialog(null, "Please enter the name of your amoeba colony.", 
    "Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE); 
    caretakerName = JOptionPane.showInputDialog(null, "Please enter the name of your amoeba colony's caretaker.", 
    "Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE); 
    startingSize = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the starting size of your amoeba colony.", 
    "Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE)); 
} 

public void setUserActions() 
{ 
    JOptionPane.showMessageDialog(null, "Congratulations, " + colonyName + " is starting out great with a totoal of " + startingSize + " amoebas.", 
    "Your New Amoeba Colony", JOptionPane.INFORMATION_MESSAGE); 
    JOptionPane.showMessageDialog(null, "Now that you've got your colony set up, you can do several things to it, including feeding, breeding, and giving it vitamins.", 
    "Developing Your Colony", JOptionPane.INFORMATION_MESSAGE); 
    daysFed = Integer.parseInt(JOptionPane.showInputDialog(null, "Feeding your colony allows it to breed, for each day fed, it can breed one time. How many days would you like to feed your colony?", 
    "Feeding Your Colony", JOptionPane.INFORMATION_MESSAGE)); 
    breedRequest = Integer.parseInt(JOptionPane.showInputDialog(null, "Breeding allows you to increase the size of your colony. A colony must be fed in order to breed. \nYour colony size will double for each successful breed. How many time would you like your colony to breed?", 
    "Breeding Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE)); 
} 

public void setVitamin() 
{ 
    requestVitamins = JOptionPane.showConfirmDialog(null, "Giving your colony vitamins decreases the chance of your amoebas getting sick and dying off. Would you like to give your colony vitamins?", 
    "Keeping Your Colony Healthy",JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);  
} 

public void vitaminResult() 
{ 
    if (requestVitamins == JOptionPane.YES_OPTION) 
     vitamin = true;   
    else 
     vitamin = false; 
} 

public void setBreeding() 
{ 
    if (daysFed >= breedRequest) 
     breedSuccess = breedRequest; 
    else 
     breedSuccess = daysFed; 

    colonySize = startingSize; 
    colonySize *= Math.pow(2, breedSuccess); 
} 

public void getSick() 
{ 
    Random number = new Random();   
    rNumber = number.nextInt(100); 

    if (vitamin == true) 
    { 
     if (rNumber <= 20) 
      sickness = true; 
     else 
      sickness = false; 
    } 
    else 
    { 
     if (rNumber <= 25) 
      sickness = true; 
     else 
      sickness = false; 
    } 
} 

public void sickEffect() 
{   
    if (sickness == true) 
     finalSize = colonySize*.10; 
    else 
     finalSize = colonySize; 
} 

public void deadCount() 
{ 
    died = finalSize - colonySize; 
} 

public void setSickText() 
{ 
    if (sickness == true) 
     isItSick = "Yes"; 
    else 
     isItSick = "No"; 
} 

public void getFinalDialogOutput() 
{ 
    JOptionPane.showMessageDialog(null, rNumber + " All actions have been completed. \n\n Colony Name: " + colonyName + "\n Caretaker Name: " + caretakerName + "\n Starting Size: " + startingSize + "\n Number of of times fed in days: " + daysFed + "\n Number of times requested to breed: " + breedRequest + "\n Number of successful breeds: " + breedSuccess + "\n Sickness and death: " + isItSick + ", " + died + " died" + "\n Final size of colony: " + finalSize, "Amoeba Colony Summary", JOptionPane.INFORMATION_MESSAGE); 
} 
} 

和測試類這裏

public class AmoebaColonyTest { 
public static void main(String[] args) { 

    AmoebaColony newColony = new AmoebaColony(); 

    newColony.setColonyProperties(); 
    newColony.setUserActions(); 
    newColony.setVitamin(); 
    newColony.setBreeding(); 
    newColony.getSick(); 
    newColony.sickEffect(); 
    newColony.deadCount(); 
    newColony.setSickText(); 
    newColony.getFinalDialogOutput(); 

} 
} 
+0

@clcto OK對不起,我編輯它。 – beginnercoder010812 2014-10-07 22:32:32

+0

請注意,你永遠不會調用'vitaminResult',但無論如何,我跑你的原代碼,他們有時會生病。如果有問題,我不知道問題是什麼。 – Radiodef 2014-10-07 22:37:21

回答

0

假設它不是一個邏輯錯誤,我的猜測是始終使用大括號,使知道如果語句正確執行。此外,你正在使用隨機數字,你仍然有1/5的東西生病。確保你做了多次迭代並打印rNumber。

if (vitamin == true && rNumber <= 20) 
     sickness = true; 

    else if (vitamin == false && rNumber <= 25) 
     sickness = true; 

    else 
     sickness = false; 

嘗試這樣的:

if (vitamin == true && rNumber <= 20){ 
     sickness = true; 
    } 
    else if (vitamin == false && rNumber <= 25){ 
     sickness = true; 
    } 
    else{ 
     sickness = false; 
    } 
+0

好的,我做到了,謝謝。但是當我產生了33個數字時,它說病症是錯誤的,並且在輸出中,isItSick被設置爲「否」並且0死亡,當它應該是「是」和「#死亡」 – beginnercoder010812 2014-10-07 22:27:18

+0

那就好了,你的如果陳述只會讓他們生病,如果rNumber小於或等於20或25 – luisluix 2014-10-07 22:30:13

+0

好吧,對。感謝代碼修復,它現在似乎正在工作。我只需要修復我犯的數學錯誤,它給了我的消極的死亡和最終大小:P謝謝。 – beginnercoder010812 2014-10-07 22:34:30