gpt4 book ai didi

java - 在此 foreach 循环中,终端中打印出不需要的字符

转载 作者:行者123 更新时间:2023-12-01 23:28:33 25 4
gpt4 key购买 nike

我正在开发一个计算热指数的程序,并且应该大量使用 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 + " ");
}

}
}

最佳答案

您正在打印对象的集合,而不是单个对象本身。例如:for (double data : keyWestTemp) { System.out.print(keyWestTemp + " ");
}

实际上应该是:for (double data : keyWestTemp) {
System.out.print(data + " ");
}

关于java - 在此 foreach 循环中,终端中打印出不需要的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com