對於我的任務之一,我已計算的熱量指數和使用的printf格式化輸出到整齊地顯示,像這樣:printf的上字符串的數組來格式化輸出
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
________________________________________________________________________________________
Temperature (F): 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10 11 12
Humidity (%): 1 2 3 4 5 6 7 8 9 10.3 11.2 12.1
HI (F): 1.1 2.2 3.3 4.4 5 7 6 8 9 10 11 12
問題是,我不知道如何格式化字符串數組,因爲字符串數組包含數字。在我的程序中,是否必須將我聲明爲String的數組轉換爲double或float類型,然後使用printf格式化它?另外,我不知道如何使用數組進行計算。在我的任務中,我必須使用兩個數組來計算熱指數。爲了解決這個問題,我嘗試了通過索引單獨執行計算。問題是,該程序將只顯示整個陣列。該程序正在讀取兩個文件並將文本存儲在一個數組中,每個文件一個數組。任何幫助將不勝感激。所述第一文件包含此:
70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0
和第二包含此:
和我的代碼是這樣的:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author timothylee
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class HeatIndex {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
// create months
System.out.println("Jan" + " Feb" + " Mar" + " April" + " May" + " June" +
" July" + " Aug" + " Sep" + " Oct" + " Nov" + " Dec");
// create line
System.out.println("_________________________________________________"
+ "__________________________________");
// // read KeyWestTemp.txt
// create token1
String token1 = "";
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File
("/Users/timothylee/KeyWestTemp.txt")).
useDelimiter(",\\s*");
// create temps1
List<String> temps1 = new LinkedList<String>();
// while loop
while(inFile1.hasNext()){
// find next
token1 = inFile1.next();
// initialize temps1
temps1.add(token1);
}
// close inFile1
inFile1.close();
// create array
String[] tempsArray1 = temps1.toArray(new String[0]);
// for-each loop
for(String s : tempsArray1){
// display Temp (F)
System.out.print("Temp (F) ");
// display s
System.out.printf(tempsArray1[0]);
// create new line
System.out.println();
}
// create token2
String token2 = "";
// create Scanner inFile2
Scanner inFile2 = new Scanner(new File
("/Users/timothylee/KeyWestHumid.txt")).
useDelimiter(",\\s*");
// create temps2
List<String> temps2 = new LinkedList<String>();
// while loop
while(inFile2.hasNext()){
// find next
token2 = inFile2.next();
// initialize temps2
temps2.add(token2);
}
// close inFile2
inFile2.close();
// create array
String[] tempsArray2 = temps2.toArray(new String[0]);
// for-each loop
for(String ss : tempsArray2){
// create Humidity (%)
System.out.print("Humidity (%) ");
// display ss
System.out.printf(tempsArray2[0]);
}
// calculate heat index
}
}
和我的輸出是這樣的:
run:
Jan Feb Mar April May June July Aug Sep Oct Nov Dec
___________________________________________________________________________________
Temp (F) 70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0
Humidity (%) 69 67 66 64 66 69 67 67 70 69 69 70BUILD SUCCESSFUL (total time: 0 seconds)
在第一個代碼示例中,format方法需要四個字符串。 – broncoAbierto
是的,我假設他正在使用包含縮寫12個月的字符串數組。 – NitroNbg
好的。我沒有想到這一點。如果您編輯答案,我可以刪除downvote。 – broncoAbierto