对于我的一项任务,我必须计算热量指数并使用 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
问题是,我不知道如何格式化字符串数组,因为字符串数组包含数字。在我的程序中,我是否必须将我声明为字符串的数组转换为双精度或浮点数,然后使用 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
第二个包含:
69 67 66 64 66 69 67 67 70 69 69 70
我的代码是这样的:
/*
* 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)