Java - 代码编译但整个代码不运行

时间:2012-12-11 02:17:13

标签: java runtime

我正在进行一项任务,我需要找到较小物种的种群数量超过较大物种种群的时间。

我创建的程序编译,但只运行到上半部分。在if-else循环开始之前,它没有输出两个system.out.println命令之后的任何内容。

我需要采取哪些步骤来调整程序代码,使其成功运行if-else循环?

CLASS:

import java.util.Scanner;

public class Species
{
    private String name;
    private int population;
    private double growthRate;

    public void readInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is the species' name?");
        name = keyboard.nextLine();

        System.out.println("What is the population of the species?");
        population = keyboard.nextInt();

        while(population < 0)
        {
            System.out.println("Population cannot be negative.");
            System.out.println("Reenter population:");
            population = keyboard.nextInt();
        }

        System.out.println("Enter growth rate (% increase per year):");
        growthRate = keyboard.nextDouble();
    }

    public void writeOutput()
    {
        System.out.println("Name = " + name);
        System.out.println("Population = " + population);
        System.out.println("Growth rate = " + growthRate + "%");
    }

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

    public Species(String name)
    {
        name = name;
        population = 0;
        growthRate = 0.0;
    }

    public Species(int population)
    {
        name = "";
        if (population > 0)
            population = population;
        else
        {
            System.out.println("ERROR: using a negative" + "population.");
            System.exit(0);
        }
        growthRate = 0.0;
    }

    public Species(double growthRate)
    {
        name = "";
        population = 0;
        growthRate = growthRate;
    }

    public Species(String name, int population, double growthRate)
    {
        name = name;
        if (population > 0)
            population = population;
        else
        {
            System.out.println("ERROR: using a negative" + "population.");
            System.exit(0);
        }
        growthRate = growthRate;
    }

    public Species()
    {
        name = "";
        population = 0;
        growthRate = 0;
    }

    public void setSpecies(String newName, int newPopulation, double newGrowthRate)
    {
        name = newName;
        if (newPopulation >= 0)
            population = newPopulation;
        else
        {
            System.out.println("ERROR: using a negative " + "population.");
            System.exit(0);
        }

        growthRate = newGrowthRate;
    }

    public void setName(String name)
    {
        name = name;
    }

    public void setPopulation(int population)
    {
        if (population > 0)
            population = population;
        else
        {
            System.out.println("ERROR: using a negative" + "population."); 
            System.exit(0);
        }
    }

    public void setGrowthRate(double growthRate)
    {
        growthRate = growthRate;
    }

    public String getName()
    {
        return name;
    }

    public int getPopulation()
    {
        return population;
    }

    public double getGrowthRate()
    {
        return growthRate;
    }

    public boolean equals(Species otherObject)
    {
        return (name.equalsIgnoreCase(otherObject.name)) &&
               (population == otherObject.population) &&
               (growthRate == otherObject.growthRate);
    }
}

方案:

import java.util.Scanner;
public class KlingonOx extends Species
{
    public static void main(String[] args) 
    {  
        new KlingonOx().run();
    }

    public void run()
    {
        Species klingonSpecies = new Species("Ox", 100, 15);
        Species earthSpecies = new Species("Elephant", 10, 35);

        klingonSpecies.readInput();
        klingonSpecies.writeOutput();
        klingonSpecies.predictPopulation(10);
        klingonSpecies.setSpecies("Ox", 100, 15);
        klingonSpecies.setName("Ox");
        klingonSpecies.setPopulation(100);
        klingonSpecies.setGrowthRate(15);
        klingonSpecies.getName();
        klingonSpecies.getPopulation();
        klingonSpecies.getGrowthRate();
        klingonSpecies.equals(earthSpecies);

        earthSpecies.readInput();
        earthSpecies.writeOutput();
        earthSpecies.predictPopulation(10);
        earthSpecies.setSpecies("Elephant", 10, 35);
        earthSpecies.setName("Elephant");
        earthSpecies.setPopulation(10);
        earthSpecies.setGrowthRate(35);
        earthSpecies.getName();
        earthSpecies.getPopulation();
        earthSpecies.getGrowthRate();
        earthSpecies.equals(klingonSpecies);

        System.out.println("The " + klingonSpecies.getName() + " starting population is: " + klingonSpecies.getPopulation());
        System.out.println("The " + earthSpecies.getName() + " starting population is: " + earthSpecies.getPopulation());


        int year = 0;
        int newkpop = 100;
        int newepop = 10;
        if (klingonSpecies.getPopulation() < earthSpecies.getPopulation())
        {
            while (klingonSpecies.getPopulation() < earthSpecies.getPopulation())
            {
                newkpop = (int)(klingonSpecies.getPopulation() + (klingonSpecies.getPopulation()*1.15) );
                newepop = (int)(earthSpecies.getPopulation() + (earthSpecies.getPopulation()*1.35) );
                year ++;
            }
            System.out.println("The population of " + klingonSpecies.getName() + " exceeds the population of " + earthSpecies.getName() + " in " + year + "years.");
        }
        else
        {
        while (klingonSpecies.getPopulation() > earthSpecies.getPopulation())
            {
                newkpop = (int)(klingonSpecies.getPopulation() + (klingonSpecies.getPopulation()*1.15) );
                newepop = (int)(earthSpecies.getPopulation() + (earthSpecies.getPopulation()*1.35) );
                year ++;
            }
            System.out.println("The population of " + earthSpecies.getName() + " exceeds the population of " + klingonSpecies.getName() + " in " + year + "years.");
        }

    }
}

3 个答案:

答案 0 :(得分:1)

您有一个Species#setPopulation()方法,但看起来您忘了使用它。在两个循环中使用klingonSpecies.setPopulation(newkpop);earthSpecies.setPopulation(newepop);

与循环问题无关:您可能希望使用Species#getGrowthRate()而不是输入数字1.15和1.35。

答案 1 :(得分:0)

问题是你永远不会更新你的物种种群:

klingonSpecies.setPopulation(newkpop);
earthSpecies.setPopulation(newepop)

答案 2 :(得分:0)

首先要注意,你永远不会改变循环中任何一个物种的种群,因此人口实际上保持不变。

在进行计算时,您需要将更改应用于对象的实例,大概使用setPopulation方法或者您需要维护每个物种的单独变量,这些变量将当前群体作为循环进行。