0

我有一个计算热量指数的方程。老师提供了方程,我把它复制对了,但它没有看我的湿度变量。(对不起,如果我把这个贴错了,这是我在这里的第一篇文章)

public class HeatIndexCalculator {

  private int temperature;
  private double humidity;
  private double heatIndex;
  public double calculateHeatIndex(int currentTemp, double currentHumidity){
     temperature = currentTemp;
     humidity = (currentHumidity/100.0);
     heatIndex=(-42.379)+(2.04901523*temperature)+10.14333127*humidity+         
            -0.22475541*temperature*humidity+                                       
            -0.00683783*temperature*temperature+                                    
            -0.05481717*humidity*humidity+                                      
            0.00122874*temperature*temperature*humidity+                            
            0.00085282*temperature*humidity*humidity+                               
            -0.00000199*temperature*temperature*humidity*humidity;  

下面是我尝试调用该方法时

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
    int x;
    double y,z;
    HeatIndexCalculator HeatC = new HeatIndexCalculator();
    System.out.println("Please enter a temperature in degrees Fahrenheit");
    x = input.nextInt();
    System.out.println("Please enter the current humidity as a percenage");
    y = input.nextDouble();
    z = HeatC.calculateHeatIndex(x, y);
   HeatC.printHeatIndex(x, y, z);
}           
4

2 回答 2

0

你的方法没有问题。我稍微扩展了您的代码以生成一个可执行示例。输出是:

temperature=20 humidity=50 : -1.0736070574999943
temperature=20 humidity=25 : -2.601307430624994
temperature=20 humidity=75 : 0.44927371937500604

所以注意湿度就好了。我怀疑您的问题实际上出在您的测试中,因为您使用了两种不同的湿度单位,一个比另一个大 100 倍。

于 2013-02-06T23:47:38.310 回答
0

该解决方案有效,但可能精度不够:

public static double calculateHeatIndex(int currentTemp, double currentHumidity)
{
    int temperature = currentTemp;
    double humidity = (currentHumidity);
    double heatIndex = (-42.379) + (2.04901523 * temperature) + 10.14333127 * humidity + -0.22475541 * temperature * humidity
            + -0.00683783 * temperature * temperature + -0.05481717 * humidity * humidity + 0.00122874 * temperature
            * temperature * humidity + 0.00085282 * temperature * humidity * humidity + -0.00000199 * temperature
            * temperature * humidity * humidity;
    return heatIndex;
}

@Test
public void testConst()
{
    System.out.println(String.valueOf(calculateHeatIndex(95, 55)));
}

结果 :

108.94701570000008
于 2013-02-06T23:50:29.090 回答