我正在尝试创建一个程序来计算有关蝴蝶种群的不同数据。我的主要问题是我不断收到我的potentialPopulation
方程式错误,我将我的值平方ratioFactor
。错误是我正在平方的值“可能尚未初始化”。我知道我需要将 设置ratioFactor
为一个值,但是在输入输入之前该值是未知的。另外,我是初学者,所以如果有人看到任何其他错误,我将非常感谢任何帮助。谢谢
// 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));
}
}