我正在开发一个计算热量指数的程序,并且应该大量使用 foreach 循环。但是,当我打印到终端时,它并不正确。我已经花了两天的时间,但我仍然不知道为什么它仍然这样做。感谢您的任何帮助/建议!
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class HeatIndex {
public static void main(String[] args) throws IOException {
Scanner keyWestHumidScan = new Scanner(new File("KeyWestHumid.txt"));
Scanner keyWestTempScan = new Scanner(new File("KeyWestTemp.txt"));
int counter1 = 0;
int counter2 = 0;
double[] keyWestHumid = new double[12];
double[] keyWestTemp = new double[12];
String header1 = " Heat index: Key West, Florida ";
String header2 = "\n Months \n ";
String[] months = {" Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec ", "Avg \n"};
String header3 = "*****************************************************************************************";
String temp = "Temp (F) ";
String humid = "Hudimitiy (%) ";
String heatIndexHeader = "HI (F) ";
//read keyWestHumid into array
while (keyWestHumidScan.hasNext()) {
String data1_parse = keyWestHumidScan.next();
double data1 = Double.parseDouble(data1_parse);
keyWestHumid[counter1] = data1;
counter1++;
}
//read keyWestTemp into array
while (keyWestTempScan.hasNext()) {
String data2_parse = keyWestTempScan.next();
double data2 = Double.parseDouble(data2_parse);
keyWestTemp[counter2] = data2;
counter2++;
}
System.out.println(header1);
System.out.print(header2);
for (String headData : months) {
System.out.print(headData);
}
System.out.println(header3);
System.out.print(temp);
for (double data : keyWestTemp) {
System.out.print(keyWestTemp + " ");
}
System.out.println();
System.out.print(humid);
for (double data : keyWestHumid) {
System.out.print(keyWestHumid + " ");
}
System.out.println();
System.out.print(heatIndexHeader);
counter1 = 0;
counter2 = 0;
for (int counter3 = 0; counter3 <= 12; counter3++) {
double heatIndex = (-42.379 + (2.04901523 * keyWestTemp[counter1]) + (10.14333127 * keyWestHumid[counter2]) - (0.22475541 * keyWestTemp[counter1] * keyWestHumid[counter2]) - (0.00683783 * (keyWestTemp[counter1] * keyWestTemp[counter1])));
heatIndex = heatIndex + (-0.05481717 * (keyWestHumid[counter2] * keyWestHumid[counter2]) + (0.00122874 * (keyWestTemp[counter1] * keyWestTemp[counter1] * keyWestHumid[counter2])) + 0.00085282 * keyWestTemp[counter1] * (keyWestHumid[counter2] * keyWestHumid[counter2]) - (0.00000199 * (keyWestTemp[counter1] * keyWestTemp[counter1]) * (keyWestHumid[counter2] * keyWestHumid[counter2])));
counter1++;
counter2++;
counter3++;
System.out.print(heatIndex + " ");
}
}
}