java - 如何使用带有哨兵的while循环重复输入
问题描述
我需要有关在此代码上放置 while 循环的位置的帮助。几周前我刚开始学习 Java。我想写一个哨兵值-1,如果输入,程序将退出。只要用户输入不是-1,就一直要求重复程序。
当我将 while 循环"while(currentPop != -1)"放在第一个问题“输入当前人口”下面时,程序成功运行了它的第一门课程。但是,它并没有回到第一个问题。相反,它直接进入第二个问题,“输入出生率:”。
我应该如何继续并确保第一个问题在循环运行后不断被问到?
谢谢你们!
import java.util.Scanner;
public class Population
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double birthRate, deathRate;
double currentPop = 0;
double newPop;
long years;
System.out.print("Enter current population or -1 to exit: ");
currentPop = scan.nextDouble();
System.out.print("Enter birth rate: ");
birthRate = scan.nextDouble();
System.out.print("Enter death rate: ");
deathRate = scan.nextDouble();
newPop = 0;
years = 0;
System.out.println("===================");
System.out.println("YEAR POPULATION");
System.out.println("===================");
if (birthRate > deathRate) {
System.out.printf("0 %,15d\n", (int)currentPop);
double growthRate = (birthRate - deathRate);
double doublingTime = Math.log(2) /
Math.log(1 +(growthRate/100));
for (years = 1; years <= (doublingTime+1); years++) {
newPop = ((growthRate/100) * currentPop) + currentPop;
currentPop = newPop;
System.out.printf("%,d %,15d\n",years,(int)currentPop);
}
System.out.printf("\nIt will take %,d years to reach double "
+ "the population of %,d\n\n",
(int)(doublingTime + 1),(int)currentPop);
} else if (birthRate < deathRate) {
System.out.printf("0 %,15d\n", (int)currentPop);
double growthRate = (birthRate - deathRate);
double decreaseTime = Math.log(1/currentPop)
/ Math.log(1 + (growthRate/100));
for (years = 1; years < (1 + decreaseTime) ; years++) {
newPop = ((growthRate/100) * currentPop) + currentPop;
currentPop = newPop;
System.out.printf("%,d %,15d\n",years,(int)currentPop);
}
System.out.printf("\nPopulation will be zero in %,d years.\n",
(int)decreaseTime + 1);
} else if(birthRate == deathRate) {
System.out.printf("0 %,15d\n", (int)currentPop);
double growthRate = (birthRate - deathRate);
double decreaseTime = Math.log(1/currentPop)
/ Math.log(1 + (growthRate/100));
for (years = 1; years < (1 + decreaseTime) ; years++) {
newPop = ((growthRate/100) * currentPop) + currentPop;
currentPop = newPop;
System.out.printf("%,d %,15d\n",years,(int)currentPop);
}
System.out.printf("\nPopulation is stable.");
}
}
}
解决方案
退出程序
只需从 main 方法返回
System.out.print("Enter current population or -1 to exit: ");
currentPop = scan.nextDouble();
if (currentPop == -1.0) {
return;
}
System.out.print("Enter birth rate: ");
通过退出或重新启动循环程序
在循环构造体中使用break
或continue
while (true) {
System.out.print("Enter current population or -1 to exit: ");
currentPop = scan.nextDouble();
if (currentPop == -1) {
break;
} else if (currentPop <= 0) {
System.out.println("Population must be positive");
continue; // restart the loop
}
System.out.print("Enter birth rate: ");
...
}
System.out.println("Done!");
不接受的功能-1
您可以使用方法抽象出重复的任务(多个输入)
public static double getValue(Scanner s, String msg) {
double value = -1;
while (value == -1) {
System.out.print(msg);
value = s.nextDouble();
}
return value;
}
在主要方法中
currentPop = getValue(scan, "Enter current population: ");
birthRate = getValue(scan, "Enter birth rate: ");
推荐阅读
- swift - 如何更改标签的视图
- string - GNAT 18.1 将字符视为字符串
- python - TensorFlow while 循环,其中预测是下一次迭代的输入
- php - 绕过摘要身份验证中间件(zend expressive)?
- google-maps - 找不到符号导入 com.google.android.gms.maps.model.StreetViewSource?
- java - 如何在一个方法中调用 2 个不同的方法
- android - 使用 Android 导航组件的条件返回导航
- php - 登录后如何从数据库中动态显示用户名?
- spring - 使用非默认端口运行 Spring Boot Admin
- javascript - 使用 json 数据的 CanvasJs 图表