尝试使while循环正常工作

时间:2016-10-08 22:20:40

标签: java eclipse math while-loop

我制定了一个计算开始年(2011年)一年人口的计划,每年增加1.2%的人口。 2011年的人口是7.000(我使用小数而不是数十亿)。这是我的代码的工作部分

package src;

import java.util.Scanner;

import java.text.DecimalFormat;

public class Demographics {

    public static void main(String[] args) {
        Scanner user_input= new Scanner(System.in);

        //Part 1

        System.out.println("===--- Part 1 ---===");
        System.out.println("Population in 2011: 7.000");
        System.out.print("What is the desired year? ( > 2011) ");
        int startYear = 2011;
        int endYear = user_input.nextInt();
        while (endYear <= startYear){
            System.out.println("Invalid end year.");
            System.out.print("What is the desired year? ( > 2011) ");
            endYear = user_input.nextInt();
            break;
        }
        double t = 0.012; 
        double nbr = (endYear - startYear); 
        double pStart = 7.000; 
        double pEnd = pStart * Math.exp(nbr * t);
        DecimalFormat nf = new DecimalFormat("#.000");
        System.out.println("Population in " + endYear + ":(nf.format(pEnd)));

    //Part 2

    System.out.println("===--- Part 2 ---===");
    System.out.print("What is the target population? ( > 7.000) ");
    double pTarget = user_input.nextDouble();
    while (pTarget <= pStart){
        System.out.println("Invalid target population.");
        System.out.print("What is the target population? ( > 7.000) ");
        pTarget = user_input.nextDouble();
        break;
    }
    while (pStart < pTarget){
        startYear++;
        pStart = pStart + (pStart * 0.012);
        System.out.println("Population in " + startYear + ": " + nf.format((pStart)));
    }
}
}

我的代码的第1部分计算用户输入一年的人口数量,然后第2部分显示用户输入人口到达该点时需要多少年的计算。

以下是无法运作的代码

//Part 3

    System.out.println("===--- Part 3 ---===");
    t = 1.2;
    pStart = 7.000;
    pEnd = pStart * Math.exp(nbr * t);
    while (pStart < pTarget){
        startYear++;
        pEnd = pStart + (pStart * 0.012);
        if (pEnd >= pStart * 2 ){
        System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
        }else{
            System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
        }
  }

目前,当我的代码中有第3部分时,它会进行无限循环而不会增加总体。我在第3部分尝试做的事情与第2部分完全相同,但在第3部分中,它将显示人口增长率(t),并在每次人口增加一倍时将其除以2。例如:

2019年人口:7.705;人口增长率:1.2%

2020年人口:7.798;人口增长率:1.2%

2021年人口:7.892;人口增长率:1.2%

...

2068年人口:13.873;人口增长率:1.2%

2069年人口:14.040;人口增长率:0.6%

任何人对如何实现这一点都有任何想法?

3 个答案:

答案 0 :(得分:3)

如果你在循环中遇到问题,原因就在于条件!

while (pStart < pTarget){

所以要结束这段代码,在循环内部必须发生这种情况:(根据你的情况)

A)pStart值应增加

B)pTarget应该减少值

C)“休息”;必须发生

在您的代码中增加:startYear和pEnd,但这不是根据您的条件关闭循环的条件。 (我之前写的:A,B,C)

1)startYear也不会在循环之前重新初始化它,并且已经以高值开始。你必须添加倦怠循环:

startYear = 2011;

2)你应该尽可能为段3创建新变量,不要像刚刚描述的那样出现问题,要清楚你正在做什么。

我对第三部分的建议如下: (考虑到我不清楚我想要阅读你的代码,你必须把它搞砸并让它对你有好处)

System.out.println("===--- Part 3 ---===");
        t = 1.2;
        startYear = 2011; // I add it
        double pEveryYear = 7000;
        while (pEveryYear < pTarget){
            startYear++;
            pEveryYear = pEveryYear + (pEveryYear * 0.012);
            if (pEveryYear >= pTarget  ){  // this condition cange only the print in the console
                System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + (t / 2));
                break; // if you write it before the system.out you can't read it in the console.
            }else{
                System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + t);

            }
      }
    }

这是输入控制台输出,如“8000”:

===--- Part 3 ---===
Population in 2012: 7084.000 Population growth rate : 1.2
Population in 2013: 7169.008 Population growth rate : 1.2
Population in 2014: 7255.036 Population growth rate : 1.2
Population in 2015: 7342.097 Population growth rate : 1.2
Population in 2016: 7430.202 Population growth rate : 1.2
Population in 2017: 7519.364 Population growth rate : 1.2
Population in 2018: 7609.596 Population growth rate : 1.2
Population in 2019: 7700.912 Population growth rate : 1.2
Population in 2020: 7793.323 Population growth rate : 1.2
Population in 2021: 7886.842 Population growth rate : 1.2
Population in 2022: 7981.485 Population growth rate : 1.2
Population in 2023: 8077.262 Population growth rate : 0.6

答案 1 :(得分:0)

您尝试比较的while循环中似乎有两个变量但是在while循环中迭代时这些变量实际上并没有更新。因此,在循环内的每次迭代之后,while条件才会保持为真。 请参阅下面的最后几行,对第3部分代码进行细微更改:

System.out.println("===--- Part 3 ---===");
t = 1.2;
pStart = 7.000;
pEnd = pStart * Math.exp(nbr * t);
while (pStart < pTarget){
    startYear++;
    pEnd = pStart + (pStart * 0.012);
    if (pEnd >= pStart * 2 ){
        System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
    } else {
            System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
    }
    pStart = startYear; // <-- NEW, you can probably use pStart instead of startYear in this code
}

答案 2 :(得分:0)

我在代码中看到的唯一缺失的是,每次进行循环时都不会更新pStart。这不仅会使你的循环变得无限,而且除了第一次迭代之外,它每次都会进行错误的计算。我在代码中只添加了一行:

System.out.println("===--- Part 3 ---===");
t = 1.2;
pStart = 7.000;
pEnd = pStart * Math.exp(nbr * t);
while (pStart < pTarget){
    startYear++;
    pEnd = pStart + (pStart * 0.012);
    if (pEnd >= pStart * 2 ){
        System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
    }else{
        System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
    }
    pStart = pEnd;
}