Java 尝试使while循环正常工作

Java 尝试使while循环正常工作,java,eclipse,math,while-loop,Java,Eclipse,Math,While Loop,我正在制作一个程序,计算一年(2011年)的人口,并每年增加1.2%的人口。2011年的人口是7000(我用的是小数,而不是数十亿)。这是我代码的工作部分 package src; import java.util.Scanner; import java.text.DecimalFormat; public class Demographics { public static void main(String[] args) { Scanner user_inp

我正在制作一个程序,计算一年(2011年)的人口,并每年增加1.2%的人口。2011年的人口是7000(我用的是小数,而不是数十亿)。这是我代码的工作部分

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)));
    }
}
}
包src;
导入java.util.Scanner;
导入java.text.DecimalFormat;
公共阶层人口统计{
公共静态void main(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
//第一部分
System.out.println(“==---第1部分---==”;
System.out.println(“2011年人口:7.000”);
系统输出打印(“期望的年份是什么?(>2011)”);
int startYear=2011;
int endYear=user_input.nextInt();
而(年底7.000);;
double pTarget=user_input.nextDouble();

while(pTarget在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
}
System.out.println(“==---第3部分---==”;
t=1.2;
pStart=7.000;
pEnd=pStart*Math.exp(nbr*t);
while(pStart=pStart*2){
System.out.println(“人口在”+startYear+“:”+nf.format((pEnd))+“人口增长率”+“:”+(t/2));
}否则{
System.out.println(“人口在“+startYear+”:“+nf.format((pEnd))+”人口增长率“+”:“+t”);
}

pStart=startYear;//似乎在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
}
System.out.println(“==---第3部分---==”;
t=1.2;
pStart=7.000;
pEnd=pStart*Math.exp(nbr*t);
while(pStart=pStart*2){
System.out.println(“人口在”+startYear+“:”+nf.format((pEnd))+“人口增长率”+“:”+(t/2));
}否则{
System.out.println(“人口在“+startYear+”:“+nf.format((pEnd))+”人口增长率“+”:“+t”);
}

pStart=startYear;//如果循环中存在问题,则原因在于条件

while (pStart < pTarget){
2) 你应该尽可能地为第3段创建新的变量,而不是像刚才描述的那样出现问题,而是要清楚你在做什么

我对第三部分的建议是: (考虑到我不清楚阅读您的代码时我想做什么,您必须将其存储起来并使其对您有利)


如果循环中存在问题,原因在于条件

while (pStart < pTarget){
2) 你应该尽可能地为第3段创建新的变量,而不是像刚才描述的那样出现问题,而是要清楚你在做什么

我对第三部分的建议是: (考虑到我不清楚阅读您的代码时我想做什么,您必须将其存储起来并使其对您有利)


我在您的代码中看到的唯一缺失是,您没有在每次执行循环时更新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;
}
System.out.println(“==---第3部分---==”;
t=1.2;
pStart=7.000;
pEnd=pStart*Math.exp(nbr*t);
while(pStart=pStart*2){
System.out.println(“人口在”+startYear+“:”+nf.format((pEnd))+“人口增长率”+“:”+(t/2));
}否则{
System.out.println(“人口在“+startYear+”:“+nf.format((pEnd))+”人口增长率“+”:“+t”);
}
pStart=pEnd;
}

我在您的代码中看到的唯一缺失是,您没有在每次执行循环时更新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;
}
System.out.println(“==---第3部分---==”;
t=1.2;
pStart=7.000;
pEnd=pStart*Math.exp(nbr*t);
while(pStart=pStart*2){
System.out.println(“人口在”+startYear+“:”+nf.format((pEnd))+“人口增长率”+“:”+(t/2));
}否则{
System.out.println(“人口在“+startYear+”:“+nf.format((pEnd))+”人口增长率“+”:“+t”);
}
pStart=pEnd;
}

好吧,除非我读错了,否则你永远不会在循环中更改pStart。那么,如何满足循环退出条件?我会对循环中的pStart更改什么?可能没有。这很可能意味着你的循环条件没有意义。请逐行检查你的代码。它应该是
而不是(pEnd
?当我有时间时(pEndwhile(pEnd
?当我有while(pEndSystem.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; }