我想創建一個計算關於蝴蝶種羣的不同數據的程序。我的主要問題是,我一直收到我的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));
}
}
一定要接受一個幫助你的答案,或評論答案沒有幫助你的方式! –