简体   繁体   中英

Having a beginner issue with some basic math functions in Java. Anything I'm doing wrong?

EDIT: Thanks a ton for everyone who has helped. I can't believe I missed that single typo.

I'm in need of help for some basic math in Java. I'm trying to create a heat index calculator but when Java compiles runs the program the calculator seems to slip up. I've checked it over multiple times to see the problem but cant find anything. Help would be greatly appreciated.

Code:

    double t = 84.3; //temperature
    int h = 81;    //humidity
    //the following are parts of the heat index formula
    double c1 = -42.379;
    double c2 = 2.04901523;
    double c3 = 10.14333127;
    double c4 = -0.22475541;
    double c5 = -.00683783;
    double c6 = -5.481717E-2;
    double c7 = 1.22874E-3;
    double c8 = 8.5282E-4;
    double c9 = -1.99E-6;
    double hI = c1 + c2 * t + c3 * h + c4 * t * h + c5 * t*t + c6 * h*h + c7 * t*t * h + c8 * t + h*h + c9 * t*t * h*h; //formula for calculating heat index
    System.out.println(hI);

So the problem here is that instead of giving me the desired output (heat index), it's returning a very large number (6184.5981258548). Is there an issue with my code? Once again, any help would be appreciated. Thanks.

According to the Heat Index Wikipedia page , then c8 term should be:

c 8 TR 2

But you have for that term:

c8 * t + h*h 

Try

c8 * t * h*h 

Making this change, I get as output:

95.21440841480018

您在c8*t + h*h有错字应该是c8*t*h*h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM