0

私はまったくの初心者で、Java の基本さえほとんど知りません (クラスに参加してまだ 2 週間しか経っていません)。私の最初の課題は、Eclipse を使用して Java で、ユーザーが指定した現在の温度と現在の湿度に基づいて熱指数を計算することです。コードを思いついたのですが、役に立ちませんでした。私のコードでは、ユーザーに温度と湿度の入力を求めていますが、結果は出力されません。コードを作成するために必要な UML ダイアグラムを提供しました。これにより、私が何をしたのかをよりよく理解できるようになります。最終的に、私の問題は、さまざまなメソッドとの間で値をやり取りするプロセスのどこかにあると思います...見て、おそらく私を正しい方向に導いてくれる人はいますか?

import java.util.Scanner;

public class HeatIndexCalculator1 {

    private int temperature;
    private double humidity;
    private double heatIndex;

    public static void main(String args[]) {

        //Get current temp and humidity from user
        Scanner input = new Scanner(System.in);
        System.out.printf("Please enter the current temperature in degrees Fahrenheit: ");
        int currentTemp = input.nextInt();
        System.out.printf("\nPlease enter the current humidity as a percentage: ");
        double currentHumidity = input.nextDouble();
    }

    private double calculateHeatIndex ( int currentTemp, double currentHumidity ) {
        //Setting parameters for Function
        int temperature = currentTemp;
        double humidity = currentHumidity;
        double answer;
        final double C1 = -42.379;
        final double C2 = 2.04901523;
        final double C3 = 10.14333127;
        final double C4 = -0.22475541;
        final double C5 = -.00683783;
        final double C6 = -5.481717E-2;
        final double C7 = 1.22874E-3;
        final double C8 = 8.5282E-4;
        final double C9 = -1.99E-6;
        int T = temperature;
        double R = humidity;
        double T2 = temperature * temperature;
        double R2 = humidity * humidity;

        //Function of Calculating Heat Index
        double answer = C1 + (C2 * T) + (C3 * R) + (C4 * T * R) + (C5 * T2) + (C6 * R2) + (C7 * T2 * R) + (C8 * T * R2) + (C9 * T2 * R2);

        return answer;
        }
    private void printHeatIndex( int currentTemp, double currentHumidity, double calculatedHeatIndex) { 
        double calculatedHeatIndex = answer;

        //Print Heat Index
        System.out.println("\nAt a temperature of" + currentTemp + "and a humidity of" + currentHumidity + "percent . . .\n");
        System.out.println("\nIt feels like:" + calculatedHeatIndex + "F");
    }
}
4

1 に答える 1

0

以下のようにメインメソッドを変更する必要があります。

    public static void main(String args[]) {

            //Get current temp and humidity from user
            Scanner input = new Scanner(System.in);
            System.out.printf("Please enter the current temperature in degrees fahrenheit: ");
            int currentTemp = input.nextInt();
            System.out.printf("\nPlease enter the current humidity as a percentage: ");
            double currentHumidity = input.nextDouble();
//Creating object of HeatIndexCalculator class
            HeatIndexCalculator1 heatIndex = new HeatIndexCalculator1();
double x = heatIndex.calculateHeatIndex(..,..);
heatIndex.printHeatIndex(currentTemp,currentHumidity,x);
        }

また、HeatIndexCalculator1 クラスのメソッドはプライベートであるため、コンパイラがエラーを出す可能性があります。それらをパブリックに変更すると機能します。

于 2013-09-04T01:21:09.350 に答える