新Java程序员,这个程序有什么问题?

时间:2014-01-20 23:13:57

标签: java eclipse

它必须打印行System.out.printf("%4s%22s%24s\n","Year"...),然后停止而不打印。

public class WorldPopulationGrowth {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner input = new Scanner( System.in);
        long BasePopulation; //The base, or current population
        double GrowthRate; //The rate of increase
        double GrowthResult; // The pop after growth rate increase

        //Time to obtain the numbers needed for Calculation
        //Specifically, the growth rate and the base population
        System.out.println("Welcome to the world population calculator");
        System.out.print("Enter the current world population:");
        BasePopulation = input.nextLong();
        System.out.println("Enter the current growth rate: (e.g, 1.14% would be .0114): ");
        GrowthRate = input.nextDouble();
        int Year = 1; //I need this for the next part
        System.out.printf("%4s%22s%24s\n", "Year", "Estimated Population", "Change from prior Year");
        while (Year <= 75); {// Start of the while
            GrowthResult = BasePopulation * (1 + GrowthRate);
            System.out.printf("%4d%22d%24d\n", Year, (long) GrowthResult, (long) GrowthResult - BasePopulation);
            BasePopulation = (long) GrowthResult;
        }//End of the while
    }//End of public static
}//End of class

2 个答案:

答案 0 :(得分:7)

while循环后你有一个分号。该分号是empty statement,而在循环中执行的内容。之后,你有anonymous block(这是你想在循环中执行的)。它等同于:

while (Year <= 75) {
    // empty
}
{
    // anonymous block
    GrowthResult = BasePopulation * (1 + GrowthRate);
    ...

修复方法是删除分号。

答案 1 :(得分:2)

尝试从此行中删除分号

while (Year <= 75); {// Start of the while

应该是:

while (Year <= 75) {// Start of the while