2012-10-26 97 views
1

我想創建一個計算關於蝴蝶種羣的不同數據的程序。我的主要問題是,我一直收到我的potentialPopulation公式的錯誤,其中我計算了我的ratioFactor值。錯誤是我平方的值「可能未被初始化」。我知道我需要將ratioFactor設置爲一個值,但在輸入輸入之前該值是未知的。此外,我是一名初學者,所以如果任何人看到任何其他錯誤,我將不勝感激任何幫助。謝謝Java錯誤 - 初始化,語法

// This program calculates butterfly population estimates 
// Inputs : males, estimated number of male butterflies 
//    females, estimated number of female butterflies 
// Outputs : total butterflies, sex ratio, variance 
// Written by: Charlie 
// Modified: Oct 26, 2012 by Daniel Kellogg 
// 

import java.util.Scanner; 
import java.text.DecimalFormat; 
public class Hwk7 { 
    public static void main (String[] args) { 
      int males, females; 

      int totalButterflies, sexRatio, ratioVariance, genderDifferences, matingPairs, growthFactor, ratioFactor, potentialPopulation, x; 

      Scanner stdin = new Scanner(System.in); 

      System.out.println("\nButterfly Estimator\n"); 
      System.out.print("Enter the estimated males population: "); 
      males = stdin.nextInt(); 
      System.out.print("Enter the estimated females population: "); 
      females = stdin.nextInt(); 

      totalButterflies = males + females; 
      sexRatio   = males/females; 
      ratioVariance  = males % females; 
      genderDifferences = males - females; 
      matingPairs  = males * females; 
      growthFactor  = (int)(Math.sqrt(matingPairs)); 

      if (sexRatio != 0){ 
        ratioFactor  = growthFactor/sexRatio; 

      if (sexRatio == 0){ 
        ratioFactor = (int)(Math.sqrt(ratioVariance)); 
      } 
      ratioFactor = x; 
      potentialPopulation = x^2; 

      System.out.println("\nTotal Butterflies: " + totalButterflies); 
      System.out.println("Sex Ratio  : " + sexRatio); 
      System.out.println("Variance   : " + ratioVariance); 
      System.out.println("Gender Differences: " + genderDifferences); 
      System.out.println("Possible Mating Pairs: " + matingPairs); 
      DecimalFormat oneDigit = new DecimalFormat("#.000"); 
      System.out.println("Growth Factor: " + growthFactor + oneDigit.format(growthFactor)); 
      DecimalFormat twoDigit = new DecimalFormat("#.0"); 
      System.out.println("Ratio Factor: " + ratioFactor + twoDigit.format(ratioFactor)); 
      DecimalFormat threeDigit = new DecimalFormat("##0"); 
      System.out.println("Potential Population: " + potentialPopulation + threeDigit.format(potentialPopulation)); 
    } 
} 
+0

一定要接受一個幫助你的答案,或評論答案沒有幫助你的方式! –

回答

1

諮詢的頭號最好的一塊,你可以得到關於編寫代碼是使每個功能完成一件事。我已經在下面對您的課程進行了重構,將您的關鍵功能分解爲他們自己的特定方法。然後,如果每種方法都有問題,則可以用該方法解決問題。

請注意,在您的sexRatio零除數情況下,您將得到您從未設置或請求輸入的變量(ratioVariance)的sqrt。然後立即將ratioFactor重置爲x - 一個也從未設置的神祕變量。

import java.util.Scanner; 
import java.text.DecimalFormat; 
public class Hwk7 { 
    private Scanner stdin = new Scanner(System.in);//This needs to be used throughout your class 

    //Do these defaults make sense? 
    private int males = 0; 
    private int females = 0; 

    private int totalButterflies = 0; 
    private double sexRatio  = 0; 
    private int ratioVariance  = 0; 
    private int genderDifferences = 0; 
    private int matingPairs  = 0; 
    private double growthFactor = 0; 
    private int potentialPopulation = 0; 


    public static double getInput(String message, int input) { 
     System.out.print(message); 
     input = stdin.nextInt(); 
    } 

    public static void main (String[] args) { 
     Hwk7 hw = new Hwk7(); 
     hw.run(); 
    } 

    public void run() { 
     System.out.println("\nButterfly Estimator\n"); 

     getInput("Enter the estimated males population: ", males); 
     getInput("Enter the estimated females population: ", females); 

     calculateResults(); 
     printResults(); 
    } 

    public void calculateResults() { 
     totalButterflies = males + females; 
     sexRatio   = males/females; 
     ratioVariance  = males % females; 
     genderDifferences = males - females; 
     matingPairs  = males * females; 
     growthFactor  = (int)(Math.sqrt(matingPairs)); 
     ratioFactor  = calculateRatioFactor(growthFactor, sexRatio); 
     potentialPopulation = x^2;//where are you getting x from!? 
    } 

    //Note in your original implementation you calculate this and then immediately 
    //change it to the value 'x'! This is clearly wrong. 
    public static double calculateRatioFactor(int growthFactor, int sexRatio) { 
     if (sexRatio == 0) { 
      return Math.sqrt(RATIOVARIANCE);//Ratio variance is never set! 
     } else { 
      return growthFactor/sexRatio; 
     } 
    } 

    public static void printResults(int males, int females) { 
     System.out.println("\nTotal Butterflies: " + totalButterflies); 
     System.out.println("Sex Ratio  : " + sexRatio); 
     System.out.println("Variance   : " + ratioVariance); 
     System.out.println("Gender Differences: " + genderDifferences); 
     System.out.println("Possible Mating Pairs: " + matingPairs); 
     DecimalFormat oneDigit = new DecimalFormat("#.000"); 
     System.out.println("Growth Factor: " + growthFactor + oneDigit.format(growthFactor)); 
     DecimalFormat twoDigit = new DecimalFormat("#.0"); 
     System.out.println("Ratio Factor: " + ratioFactor + twoDigit.format(ratioFactor)); 
     DecimalFormat threeDigit = new DecimalFormat("##0"); 
     System.out.println("Potential Population: " + potentialPopulation + threeDigit.format(potentialPopulation)); 
    } 
} 
+0

謝謝你解決了一切 –

1

你的變量x永遠不會初始化,您正在使用它

ratioFactor = x; 
potentialPopulation = x^2; 

我的事情你想做的事是

x= ratioFactor ;