Java Eclipse Heat Index

时间:2013-09-04 00:56:07

标签: java eclipse

我是一个完全的初学者,甚至不知道Java的基础知识(我只在课堂上待了两个星期)。我的第一个任务是根据用户给出的当前温度和当前湿度计算热量指数,使用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");
    }
}

1 个答案:

答案 0 :(得分: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类中的方法是私有的,如果您将它们更改为public它可以工作。