Java中的无限循环问题

时间:2012-02-19 23:26:13

标签: java for-loop infinite-loop

当我在此代码的底部运行for循环时,我得到一个无限循环,每次都在if语句中输出输出,每次i递增{不停。我很确定这是一个小问题,我无法指责......

    import java.util.Scanner;

public class HW2johnson_pp1 {

    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);

        System.out.printf("Please enter the species with the higher" + 
                          " population first\n");
        System.out.printf("Enter the name of your first species ");
        String Species1 = keyboard.nextLine();
        System.out.printf("Enter the species' population: ");
        int Pop1 = keyboard.nextInt();
        System.out.printf("Enter the species' growth rate: ");
        int Growth1 = keyboard.nextInt();

        System.out.printf("Enter the name of your second species: ");
        String Species2 = keyboard.nextLine();
        System.out.printf("Enter the species' population: ");
        int Pop2 = keyboard.nextInt();
        System.out.printf("Enter the species' growth rate: ");
        int Growth2 = keyboard.nextInt();

        if (Pop2 > Pop1) {
            System.out.printf("The first population must be higher. \n");
            System.exit(0);
        }


        Species input1 = new Species();
        input1.name = Species1;
        input1.population = Pop1;
        input1.growthRate = Growth1;

        Species input2 = new Species();
        input2.name = Species2;
        input2.population = Pop2;
        input2.growthRate = Growth2;

        if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <= 
            (input1.predictPopulation(2) - input2.predictPopulation(2))){

            System.out.printf(Species2 + " will never out-populate " + 
                              Species1 + "\n");
        }
        else {

            for (int i = 0; input2.predictPopulation(i) <= 
                            input1.predictPopulation(i); i++) {

                if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
                    System.out.printf(" will out-populate \n");
                }
            }
        }
    }
}

public int predictPopulation(int years)
        {
            int result = 0;
            double populationAmount = population;
            int count = years;
            while ((count > 0) && (populationAmount > 0))
            {
                populationAmount = (populationAmount +
                              (growthRate / 100) * populationAmount);
                count--;
            }
            if (populationAmount > 0)
                result = (int)populationAmount;

            return result;
        }

2 个答案:

答案 0 :(得分:3)

此:

        if (Pop2 > Pop1) {
            Species2 = Species1;
            Pop2 = Pop1;
            Growth1 = Growth2;
        }

没有意义。显然你想要的是“交换”两个群体的值,而只是让两个群体相同,这样对于years的任何值,predictPopulation(years)都是相同的对于这两个对象。


编辑添加:此测试:

        if ((input1.predictPopulation(1) - input2.predictPopulation(1)) < 
            (input1.predictPopulation(2) - input2.predictPopulation(2)))

不是确定物种2最终是否会超过物种1的有效方法。如果物种2具有更大的比例生长速率(growthRate),那么它最终会超过物种1即使在最初几年内,它的绝对增长率也较低。 (这只与你的无限循环切线相关 - Dampsquid指出,如果你有<=而不是<,你将不会有无限循环 - 但这是一个问题你需要修复。)

答案 1 :(得分:1)

您的问题在这里

    for (int i = 0; input2.predictPopulation(i) <= 
                    input1.predictPopulation(i); i++) {

input1的数量永远不会少于input2,这与以下之一有关:

  1. input1正在获取一个参数值,该参数值不允许它&#34;#34;#34;变得少于输入2(检查输入范围以确保可以发生这种情况,或许类似于确保输入2的增长率>输入1)

  2. 类本身有什么问题导致input1总是等于或大于input2。

  3. 编辑:既然您已经发布了predictPopulation的代码,那么它看起来就像是第一名。确保input2的增长率始终大于input1。