1

蝶の個体数に関するさまざまなデータを計算するプログラムを作成しようとしています。私の主な問題は、potentialPopulation方程式のエラーを受け取り続けることです。ここで、値を2乗し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));
    }
}
4

2 に答える 2

1

変数xが初期化されることはなく、それを使用しています

 ratioFactor = x;
 potentialPopulation = x ^ 2;

私はあなたがしたいことは

  x= ratioFactor ;
于 2012-10-26T21:41:38.997 に答える
1

コードの記述について得ることができる最も良いアドバイスは、各関数に正確に1つのことを実行させることです。以下のクラスをリファクタリングして、主要な機能を独自の特定のメソッドに分割しました。次に、各メソッドに問題がある場合、そのメソッドの問題を解決できます。

sexRatioゼロ因子の場合、ratioVariance設定したことのない変数()の平方根を取得するか、入力を要求することにも注意してください。次に、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));
    }
}
于 2012-10-26T22:00:38.543 に答える