嘿,大家所以我必須做一個任務,我們通過一些天氣數據進行排序,我完成了大部分工作,但我沒有得到我應該得到的結果。 (編輯)這是一個文件鏈接:http://ksuweb.kennesaw.edu/~bsetzer/2302sp15/extra/data/636870-99999-2013.op。
是我得到的樣子的結果:Java - 數據排序代碼,輸出不是它應該是的
Station ID: 636870
WBAN ID: 99999
Year: 2013
Max average temp: 68.0
Min average temp: 63.1
Max max temp: 89.6
Min min temp: 51.8
Average visibility: 73.18993288590602
149 lines counted.
但輸出應該是這樣的。
Station id: 636870
WBAN id: 99999
Year: 2013
Max Average Temp: 78.6
Min Average Temp: 60.8
Max Max Temp: 91.4
Min Min Temp: 51.8
Average visibility: 6.535507246376822
Number of fog days: 4
我的代碼:
public static void main(String[] args){
File infile = new File("636870-99999-2013.op");
int totalLineCount = 0;
int stnNumber = 0;
int wbanNumber = 0;
int year = 0;
double meanTemp = 0;
double mtemp = 0;
double minmt = 1000;
double maxmt = 0;
double maxTemp = 0;
double mtemp2 = 0;
double minTemp = 1000;
double mtemp3 = 0;
double aV = 0;
int count = 0;
double sum = 0.0;
try {
Scanner inscan = new Scanner(infile);
while (inscan.hasNext()) {
String line = inscan.nextLine();
String line2 = inscan.nextLine();
//STN Number
String stnNumberField = line2.substring(0, 6);
String stnNumberTrimmed= stnNumberField.trim();
stnNumber = Integer.parseInt(stnNumberTrimmed);
//wban number
String wbanNumberField = line2.substring(7, 12);
String wbanNumberTrimmed = wbanNumberField.trim();
wbanNumber = Integer.parseInt(wbanNumberTrimmed);
//year
String yearField = line2.substring(14, 18);
String yearFieldTrimmed = yearField.trim();
year = Integer.parseInt(yearFieldTrimmed);
//average temp
String meanTempField = line2.substring(24, 30);
String meanTempFieldTrimmed = meanTempField.trim();
mtemp = Double.parseDouble(meanTempFieldTrimmed);
//min average temperature
if (mtemp != 99.99 && mtemp != 999.9 && mtemp != 9999.9){
if(mtemp < minTemp){
minmt = mtemp;
}
}
if (mtemp != 9999.9){
if (mtemp > maxTemp){
maxmt = maxTemp;
}
}
//max temp
String maxTempField = line2.substring(102, 108);
String maxTempFieldTrimmed = maxTempField.trim();
mtemp2 = Double.parseDouble(maxTempFieldTrimmed);
if (mtemp2 != 9999.9){
if (mtemp2 > maxTemp){
maxTemp = mtemp2;
}
}
//min temp
String minTempField = line2.substring(110, 116);
String minTempFieldTrimmed = minTempField.trim();
mtemp3 = Double.parseDouble(minTempFieldTrimmed);
if (mtemp3 != 99.99 && mtemp3 != 999.9 && mtemp3 != 9999.9){
if(mtemp3 < minTemp){
minTemp = mtemp3;
}
}
//average visibility
String averageVisibility = line2.substring(68, 73);
String averageVisibilityTrimmed = averageVisibility.trim();
aV = Double.parseDouble(averageVisibilityTrimmed);
// read data and compute statistics
double value = aV;
sum += value;
count++;
//fog indicator
char fogIndicator = line2.getChars(132);
totalLineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
double average = sum/count;
System.out.println("Station ID: " + stnNumber);
System.out.println("WBAN ID: " + wbanNumber);
System.out.println("Year: " + year);
System.out.println("Max average temp: " + maxmt);//why is this incorrect?
System.out.println("Min average temp: " + minmt);//why is this incorrect?
System.out.println("Max max temp: " + maxTemp); //why is this output incorrect?
System.out.println("Min min temp: " + minTemp);
System.out.println("Average visibility: " + average);
//Number of fog days: 11
System.out.println(totalLineCount + " lines counted.");
} }
不知道該文件的樣子有沒有一噸我們可以提供幫助。 –
我不知道爲什麼'char fogIndicator = line2.getChars(132);'編譯,或者我應該說,它不會爲我編譯。 –
它不編譯,但我的教授說,它應該和這裏是一個鏈接到數據文件http://ksuweb.kennesaw.edu/~bsetzer/2302sp15/extra/data/636870-99999-2013.op – Draco8573