2012-12-11 115 views
0

我正在做一個任務,我需要找到多少年前,小物種的人口超過較大物種的人口。Java代碼編譯但整個代碼不運行

我創建的程序編譯,但只貫穿前半部分。在if-else循環開始之前,它沒有輸出兩個system.out.println命令之後的任何內容。

我需要採取哪些步驟來調整程序代碼,以便它能夠成功運行if-else循環?

類:

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

    } 
} 
+1

1)你看到了什麼結果? 2)你期望看到什麼? 3)爲什麼KlingonOx類擴展物種? –

+0

程序是否停止,或者控制檯是否停止工作? – Aaron

+0

使用調試器並跟蹤程序執行 – MadProgrammer

回答

1

你有Species#setPopulation()方法,但它看起來像你忘了使用它。在兩個循環中都使用klingonSpecies.setPopulation(newkpop);earthSpecies.setPopulation(newepop);

與循環問題無關:您可能想要使用Species#getGrowthRate()而不是輸入數字1.15和1.35。

0

的問題是,你永遠不更新物種的種羣:

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

開始的事實,你永遠不改變你的循環兩種物種的種羣,所以人數卻保持不變。

在進行計算時,您需要將變化應用到對象的實例,推測可能使用setPopulation方法,或者您需要爲每個物種保留單獨的變量,隨着循環的進行需要保留當前的人口。