import java.io.*;
import java.util.*;
public class HeatIndex
{
public static double [] loadKeyWestTemp() throws IOException//method to read temp text file and convert to an array
{
Scanner inFile = new Scanner(new File("KeyWestTemp.txt"));
List<Double> array = new ArrayList<>();
while(inFile.hasNext()){
array.add(inFile.nextDouble());
}
double t[] = new double[array.size()];
for(int i = 0; i < array.size(); i++){
t[i] = array.get(i);
}
inFile.close();
return t;
}
public static double [] loadKeyWestHumidity () throws IOException//method to read humidity text file and convert to array
{
Scanner inFile = new Scanner(new File("KeyWestHumid.txt"));//finding text file
List<Double> array = new ArrayList<>();//initializing Array List
while(inFile.hasNext()){//while-loop to fill array list
array.add(inFile.nextDouble());
}
double h[] = new double[array.size()];//assigning array list values to the h[] array
for(int z = 0; z < array.size(); z++){
h[z] = array.get(z);
}
inFile.close();//closing inFile
return h;//returning h[]
}
public static double heatIndex(double t, double h){//method to calculate the Heat Index formula{
//declaring variables that are a part of the Heat Index formula
double H1 = -42.379;
double H2 = 2.04901523;
double H3 = 10.14333127;
double H4 = 0.22475541;
double H5 = 6.83783 * .001;
double H6 = -5.481717 * .01;
double H7 = 1.22874 * .001;
double H8 = 8.5282 * .0001;
double H9 = 1.99 * 000001;
//the actual formula
double index = (H1) + (H2*t) + (H3 * h) + (H4 * t * h) + (H5 * (t * t)) + (H6 * (h * h)) + (H7 *(t * t)*h) + (H8 * t *(h * h)) + (H9 * (t * t)*(h * h));
return index;
}
public static void main(String[]args)throws IOException {//main method
for(int y = 0; y < 11; y++){
double HI [] = heatIndex(loadKeyWestTemp(),loadKeyWestHumidity());
}
}
}
我正在编写一个程序,从两个文本文件中读取温度和湿度百分比,然后使用里面的值从这些值中查找热量指数。我在行中出错:double HI [] = heatIndex(loadKeyWestTemp(),loadKeyWestHumidity()); 它说我不允许使用包含double []的双精度的方法。有没有办法解决这个问题,而不必重写我的程序的主要部分?谢谢!
答案 0 :(得分:1)
您需要索引双数组中的元素:
double kwt = loadKeyWestTemp();
double kph = loadKeyWestHumidity();
double HI = new double[kwt.length];
for(int y = 0; y < 11; y++){
HI[y] = heatIndex(kit[y], kph[y]);
}
这假设两个方法返回相同大小的数组(11)。