我正在研究计算热指数的程序,我应该大量使用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 + " ");
}
}
}
答案 0 :(得分:2)
您正在打印出对象集合,而不是单个对象本身。例如:for (double data : keyWestTemp) { System.out.print(keyWestTemp + " ");
}
实际应该是:for (double data : keyWestTemp) {
System.out.print(data + " ");
}
答案 1 :(得分:1)
首先,您的代码很难阅读。确保遵循良好的代码样式(例如,在for循环块中缩进4个空格)。请仔细阅读:http://www.oracle.com/technetwork/java/codeconv-138413.html
通过浏览代码,问题可以在这里:
for(double data:keyWestTemp) {
System.out.print(keyWestTemp + " ");
}
在这里:
for(double data:keyWestHumid) {
System.out.print(keyWestHumid + " ");
}
我认为您希望在打印行中使用数据,而不是keyWestHumid / Temp。 data是对象,keyWestHumid / Temp是你的整个数组。
您应该使用Eclipse或Netbeans等IDE,它会让您的生活更轻松。 Eclipse会立即告诉您数据未在这些循环中使用,这是一个问题。
答案 2 :(得分:0)
使用System.out.printf格式化要在控制台上显示的文字。
System.out.printf("%10s%10s", "Number", "String");
System.out.println("");
System.out.printf("%10d", 10);
System.out.printf("%10s", "Hi there");