0

我是一个完全的初学者,甚至几乎不知道 Java 的基础知识(我只上了两个星期的课)。我的第一个任务是根据用户给出的当前温度和当前湿度计算热量指数,在 java 中使用 Eclipse。然而,我想出了一个代码,但无济于事。我的代码确实要求用户输入温度和湿度,但它没有打印出结果。我提供了构建代码所需的 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 回答