gpt4 book ai didi

java - 如何使用带有哨兵的 while 循环重复输入

转载 作者:行者123 更新时间:2023-12-02 11:03:53 24 4
gpt4 key购买 nike

我需要关于在这段代码上放置 while 循环的帮助。我几周前才开始学习 Java。我想写一个哨兵值-1,如果输入,程序将退出。只要用户输入不是-1,就不断询问重复程序。

当我将 while 循环“while(currentPop != -1)”放在第一个问题“输入当前人口”下方时,程序成功运行了第一门类(class)。然而,它并没有回到第一个问题。相反,它直接进入第二个问题,“输入出生率:”。

我该如何继续并确保在运行完循环后不断询问第一个问题?

谢谢大家!

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.");
}
}

}

最佳答案

退出程序

只需从主方法返回

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: ");

关于java - 如何使用带有哨兵的 while 循环重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com