首页 > 解决方案 > 如何使用带有哨兵的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.");
    }
}

}

标签: javaloopswhile-loopjava.util.scanner

解决方案


退出程序

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

通过退出或重新启动循环程序

在循环构造体中使用breakcontinue

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

推荐阅读