[英]array for Fixed rate
我必須找到池塘的人口。 當用戶輸入n之后,它使用固定比率作為人口,當用戶輸入n時,它具有起始人口。最后,用戶輸入多少等級來計算當前人口。 我陷入了計算數組的困境,如何編寫代碼。有人可以幫助我嗎。
import java.util.Scanner;
public class population {
private static final int TARGET = 6000;
public static void main(String[] args) {
int iStartingPopulation,iFGeneration,iRate;
int[] iGrowthRate = new int[10];
Scanner inConsole = new Scanner (System.in);
// Read the starting population of the fish pond
System.out.println(" Enter the starting population of the Fish pond");
iStartingPopulation= inConsole.nextInt();
// Ask whether the growth rate is fixed or variable
System.out.println(" Enter F for fixed growth rate of the pond, V for Variable ");
char option = inConsole.next().charAt(0);
if ( option ==('f') || option==('F')){
//Read the fixed Growth Rate
System.out.println("Enter the fixed growth rate for genration:");
iRate = inConsole.nextInt();
//Read the fixed Growth for how many Generation
System.out.println("Enter How many Generation for:");
iFGeneration= inConsole.nextInt();
int iFCurrentPopulation= iStartingPopulation;
for (int iI =0; iI < iFiGeneration; iI++)
{
iFCurrentPopulation = iFGeneration+( iFCurrentPopulation *iRate /100);
System.out.println("Population :" +""+iFCurrentPopulation);
您的評論中幾乎包含了解決方案。 在公式中進行除法運算時,您甚至還具有保持population
不變所需的額外變量。
for (int i = 0; i < iFGeneration; i++) {
iFCurrentPopulation += ((iFCurrentPopulation * iRate) / iStartingPopulation);
}
注意:這將產生一個int
(很明顯)作為結果-好像要小數位等,應該使用double
。
例:
初始人口= 100
固定利率= 80
代= 3
將產生583
(作為int
),但實際上該數字將為583.2
(作為double
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.