Java错误 - 初始化,语法

时间:2012-10-26 21:38:50

标签: java pico

我正在尝试创建一个程序来计算有关蝴蝶种群的不同数据。我的主要问题是我的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));
    }
}

2 个答案:

答案 0 :(得分:1)

您的变量x永远不会被初始化并且您正在使用它

 ratioFactor = x;
 potentialPopulation = x ^ 2;

我想做的就是

  x= ratioFactor ;

答案 1 :(得分: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));
    }
}