2013-10-14 73 views
1

我的計劃存在一些問題,我要求用戶輸入開始人口,每日增長百分比,以及他們將會繁殖多少天。然後計算每天的結束人口數量,同時確保他們對用戶輸入數據的限制。我每天都會得到相同的結果,而且約束條件也沒有做好。隨着時間的推移人口變化

input=JOptionPane.showInputDialog("Please enter the starting number of organisms"); 
startPopulation=Double.parseDouble(input); 
input=JOptionPane.showInputDialog("Please enter their daily population increase as a percentage"); 
increase=Float.parseFloat(input); 
input=JOptionPane.showInputDialog("Please enter how many days they will multiply in"); 
daysofIncrease=Double.parseDouble(input); 
for (int days=0;days<=daysofIncrease+1;days++) 
{ 

    if (startPopulation>=2 || increase >0 || daysofIncrease>=1) 
    { 
     endPopulation=(startPopulation*increase)+startPopulation; 
     JOptionPane.showMessageDialog(null,"This is the organisms end population: "+endPopulation+" for day: "+days); 
    } 

     else 
     { 
      input=JOptionPane.showInputDialog("Please enter the starting number of organisms"); 
      startPopulation=Double.parseDouble(input); 
      input=JOptionPane.showInputDialog("Please enter their daily population increase as a percentage"); 
      increase=Float.parseFloat(input); 
      input=JOptionPane.showInputDialog("Please enter how many days they will multiply in"); 
      daysofIncrease=Double.parseDouble(input); 

     } 
     } 
     } 
    } 
+3

「請輸入** orgranisms的起始編號**」我瞥了一眼並稍微讀錯了 – Troubleshoot

+1

@排錯該評論不符合Stack Overflow的精神。 – hexafraction

+0

我以爲說*請輸入生物體的起始數字*。他看到你錯了。 – Troubleshoot

回答

0

你行

endPopulation=(startPopulation*increase)+startPopulation; 

將不能正確計算出最終的人口。你根本沒有使用daysofIncrease。

我想你需要循環的日子。請注意,我沒有測試過這一點,可能需要調整,但它應該給你的想法:

double interimPopulation = startPopulation; 
for (int days=1; days<=daysofIncrease; days++) { 
    interimPopulation *= (1.0 + (increase/100.0)); //get next day's population 
} 
endPopulation = interimPopulation; 
0

我認爲你需要的地方設置你的循環是: startPopulation = endPopulation; 然後你做的另一次迭代循環。 試試這個例如

endPopulation=(startPopulation*increase)+startPopulation;

startPopulation = endPopulation;

如果你不想失去startPopulation的初始值, 只是存儲在某個地方,你改變它之前(我建議的方式) 。

相關問題