# 請問一下我有提JAVA程式不太知道該怎麼做~~20點

Write a Java program that will prompt on the console for a user to enter the current population (population), the birth rate (birth), the death rate (death), and the number of years (years). Then compute and print out the estimated population after years years.

To assist in this, your program should contain the following static methods:

double growthRate(double birth, double death) – this method takes as its parameters the birth rate (birth) and death rate (death), and it returns the population growth rate, which is computed by the formula: ( birth – death).

int estimatedPopulation(int population, double growth, int years) – this method takes as its parameters the current population, population growth rate (growth), and the number of years (years). It returns the estimated population after years years. The estimated population at the end of a year is calculated by the formula: (population + (population * growth /100)) where population is the population at the beginning of that year.

void printResult(int currentPop, int estimatedPop, int years) – this method takes the current population, estimated population, and number of years as parameters. Then it will print the result on the console in the following format:

Current population: 320000

***

Estimated population after 6 years: 578964

******

Notice the lines of *s below the current and estimated population. These are the graphical representation of the population rounded to the nearest hundred thousand. Each * represent one hundred thousand people.

Lastly, the input birth and death rates cannot be negative, and the starting population cannot be less than 2. If the user enters an invalid value, this program will prompt user to re-enter the value until a valid value has been read.

Update:

Rating

不懂你的問題在哪?

不是敘述的很詳細嗎?

Current population: 320000 一開始的值

要有 static methods: double growthRate(double birth, double death)

要讀入the birth rate (birth)、death rate (death), returns growth rate

formula: ( birth – death)

2009-03-27 15:09:24 補充：

int estimatedPopulation(int population, double growth, int years)

formula: (population + (population * growth /100))

void printResult(int currentPop, int estimatedPop, int years)

都定義好了啊

2009-03-27 18:52:26 補充：

import java.io.*; //使用 Console 類別

import java.util.regex.*; //使用 Pattern、Matcher 兩個類別，用來判斷輸入

public class Population{

static double growthRate(double birth, double death){

return (birth-death);

}

static int estimatedPopulation(int population, double growth, int years){

for(int i=0;i<years;i++)

population = (int)(population + (population * growth /100));//這裡會失去精確度，不過是 estimated，我想應該沒關係吧

return population;

}

static void printResult(int currentPop, int estimatedPop, int years){

System.out.println("Current population: "+currentPop);

drew(Math.round(currentPop/(double)100000));

System.out.println("Estimated population after "+years+" years: "+estimatedPop);

drew(Math.round(estimatedPop/(double)100000));

}

static void drew(long loopCount){ //畫*

for(long i=0;i<loopCount;i++)

System.out.print("*");

System.out.println();

}

public static void main(String arg[]){

Console keyboardIn = System.console();

Pattern ptnRate = Pattern.compile("[0-9]{1,2}"); //至少1位數，最多2位數

Pattern ptnYears = Pattern.compile("[0-9]{1,3}");

Pattern ptnPopulation = Pattern.compile("[0-9]{2,8}");

Matcher mch = null;

String temp = null;

int currentPop = 0,estimatedPop = 0,years = 0;

double birthRate = 0,deathbirth = 0;

do{

System.out.println("Enter current population (2~8 figures number at most)：");

mch = ptnPopulation.matcher(temp);

}while(!mch.matches());

currentPop = Integer.parseInt(temp);

2009-03-27 18:56:44 補充：

do{

System.out.println("Enter the birth rate (2 figures number at most)：");

mch = ptnRate.matcher(temp);

}while(!mch.matches());

birthRate = Double.parseDouble(temp);

2009-03-27 18:58:40 補充：

貼不下 Orz 見意見吧

2009-03-27 18:58:57 補充：

do{

System.out.println("Enter the death rate (2 figures number at most)：");

mch = ptnRate.matcher(temp);

}while(!mch.matches());

deathbirth = Double.parseDouble(temp);

2009-03-27 18:59:58 補充：

do{

System.out.println("Enter the number of years (3 figures number at most)：");

mch = ptnYears.matcher(temp);

}while(!mch.matches());

years = Integer.parseInt(temp);

2009-03-27 19:00:11 補充：

estimatedPop = estimatedPopulation(currentPop,growthRate(birthRate,deathbirth),years);

printResult(currentPop,estimatedPop,years);

}

}

2009-03-27 19:00:24 補充：

/*

Math.round(currentPop/(double)100000));

Math.round()有long、int兩種return，詳見 API

currentPop/100000

599/100 會得到 5 round 後 得 5，

currentPop/(double)100000

599/100 會得到 5.99 round 後 得 6

*/

• Anonymous

兩位java的程式設計大大 - -

你們好像在拼業績餒...一直比較過來比較過去的XD

呵呵~~!!

這下子不知道發問者要選誰了...害我都不敢回答ˊˋ

• 從哪開始都ok...咳!!咳…

先從三個方法開始..............吧!

1.

double growthRate(double birth, double death){...}

2.

int estimatedPopulation(int population, double growth, int years){...}

3.

void printResult(int currentPop, int estimatedPop, int years){...}

再來就

public static void main(String arg[]){...}

2009-03-27 14:57:45 補充：

另外~

你的發問標題跟你實際給的點不一致~"~???

2009-03-27 15:02:07 補充：

英文不全好~~小部份不明

擔有曲解題意之處

如可補上中文題目近可完美

2009-03-27 18:12:30 補充：

我看發問者應該跟我一樣英文不全好

所以才不知道他的問題在哪.........吧!!

Source(s): me