我正在制作一个程序,计算给定起始年(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%
有人对如何实现这一目标有任何想法吗?
最佳答案
如果循环中出现问题,原因就在条件中!
while (pStart < pTarget){
所以要结束这段代码,在循环内必须发生这种情况之一:(根据您的条件)
A)pStart 的值应该增加
B)pTarget 的值应该递减
C)“休息”;必须发生
在您的代码中,您增加了:startYear和pEnd,但这不是根据您的条件关闭循环的条件。 (我之前写过:A、B、C)
1) startYear 也不会在循环之前重新初始化它,并且已经以较高的值开始。你必须添加bedore循环:
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
关于java - 尝试使 while 循环正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937956/