我正在编写一个程序,计算给定开始年份(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)));
}
}
}
我的代码的第一部分计算了用户输入时一年的人口,然后第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部分,它做一个无限循环,而不乘以人口。我在第三部分要做的和第二部分基本相同,但第三部分会显示人口增长率(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%
谁对如何实现这一点有任何想法?
如果在循环中出现问题,原因就在条件中!
while (pStart < pTarget){
所以要结束这段代码,在循环中必须发生以下情况之一:(根据你的条件)
A)pStart应该增加值B)pTarget值应该递减
C)"break"必须发生
在你的代码中增加: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
似乎你在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
}
我在代码中看到的唯一缺失的东西是,每次执行循环时都没有更新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;
}