Java中的无限循环问题

发布于 2025-01-06 18:27:19 字数 2700 浏览 0 评论 0原文

当我在这段代码的底部运行 for 循环时,我得到一个无限循环,每次使用 iif 语句中的输出> 每次都会增加并且不会停止。我很确定这是一个小问题,我只是无法指出......

    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;
        }

when I run the for loop towards the bottom of this code I get an infinite loop that outputs the output in the if statement every single time with i being incremented each time and doesn't stop. I'm pretty sure it's a small problem that I just can't put my finger on...

    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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2025-01-13 18:27:19

这:

        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 指出,如果您' d 有 <= 而不是 < — 但这是您需要解决的问题。)

This:

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

does not make sense. Apparently what you intend is to "swap" the values for the two populations, but instead, you just cause the two populations to be identical, so that for any value of years, predictPopulation(years) will be the same for both objects.


Edited to add: This test:

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

is not a valid way to see whether species 2 will eventually overtake species 1. If species 2 has a greater proportional growth rate (growthRate), then it will eventually overtake species 1, even if during the first few years, it has a lower absolute growth rate. (This only tangentially relates to your infinite loop — Dampsquid points out that you wouldn't have had the infinite loop if you'd had <= instead of < — but it's a problem you need to fix.)

带刺的爱情 2025-01-13 18:27:19

你的问题在这里

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

input1 的人口永远不会少于 input2,这与以下情况之一有关:

  1. input1 正在获取一个参数值,该值不允许其“人口”少于 input2(检查输入范围以确保这种情况可能会发生,例如确保 input2 的增长率 > input1)

  2. 类本身有问题,强制 input1 始终等于或大于 input2。

编辑:既然您已经发布了 PredictPopulation 的代码,它看起来就像是第一。确保输入 2 的增长率始终大于输入 1。

Your issue is here

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

The population of input1 is never becoming less then input2 this has to do with one of the following:

  1. input1 is getting a parameter value that would not allow it's "population" to become less then input2 (check ranges of inputs to make sure this can occur, maybe something like making sure that input2's growth rate is > input1)

  2. There is something wrong with the class itself that is forcing input1 to always be equal to or greater then input2.

EDIT: since you have posted the code for predictPopulation it looks like it is number one. Make sure that your growth rate for input2 is always greater then input1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文