java使用数组中声明的变量执行计算

时间:2013-11-25 18:47:33

标签: java arrays

我有一个关于如何在几个数组上执行计算的问题。我正在尝试计算热指数。当我尝试计算它时,当我运行程序时,对于热指数,它只显示0.00000。任何帮助将不胜感激。以下是我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class HeatIndex{

public static void main(String args[]) throws IOException{

    // display title
    System.out.printf("%80s %n", "Heat Index: Key West, Florida\n\n\n");

    // create months array
    String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
        "Aug", "Sep", "Oct", "Nov", "Dec"};

    // create leading blank zeros
    System.out.print("           ");

    // for each loop to display month
    for(String month : months){

        // display months
        System.out.printf("%10s", month);
    }

    // create new line
    System.out.println();

    // create line
    System.out.println("___________________________________________________"
            + "____________________________________________________________"
            + "_____________________");

    // create new line
    System.out.println();

    // create array for temp
    double[] tempA = new double[12];

    int d = 0;

    // create inFile1 variable
    Scanner inFile1 = new Scanner(new File("/Users/timothylee/KeyWestTemp.txt"));

    // while statement for inFile1
    while(inFile1.hasNextDouble()){

        // initialize array
        tempA[d++] = inFile1.nextDouble();
    }

    // display Temperature: heading
    System.out.print("Temperature:");

    // for each loop for displaying array
    for(double temperature : tempA){

        // display array
        System.out.printf("%10.1f", temperature);
    }

    // create new line
    System.out.println();

    // create inFile2 variable
    Scanner inFile2 = new Scanner(new File("/Users/timothylee/KeyWestHumid.txt"));

    // create array for humidity
    double[] humidityA = new double[12];

    int i = 0;

    // while statment for inFile2
    while(inFile2.hasNextDouble()){

        // initialize array
        humidityA[i++] = inFile2.nextDouble();
    }

    // display Humidity: heading
    System.out.print("   Humidity:");

    // for each loop for displaying array
    for(double humidity : humidityA){

        // display array
        System.out.printf("%10.1f", humidity);
    }

    // create new line
    System.out.println();

    // // perform calculation

    // create array for heat index
    double[] heatIndex = new double[12];

    int h = 0;

    // while loop for inFile1 & 2
    while(inFile1.hasNextDouble() && inFile2.hasNextDouble()){

        ////////////// initialize array
        heatIndex[h++] = -42.379 + 2.04901523 * tempA[d++] + 10.14333127 * 
                humidityA[i++] - 0.22475541 * tempA[d++] * humidityA[i++] - 
                6.83783 * Math.pow(10, -3) * Math.pow(tempA[d++], 2) - 
                5.481717 * Math.pow(10, -2) * Math.pow(humidityA[i++], 2) + 
                1.22874 * Math.pow(10, -3) * Math.pow(tempA[d++], 2) * 
                humidityA[d++] + 8.5282 * Math.pow(10, -4) * tempA[d++] * 
                Math.pow(humidityA[i++], 2) - 1.99 * Math.pow(10, -6) * 
                Math.pow(tempA[d++], 2) * Math.pow(humidityA[i++], 2);
    }

    // display Heat Index: heading
    System.out.print(" Heat Index:");

    // for each loop for displaying array
    for(double heatindex : heatIndex){

        // display array
        System.out.printf("%10.5f", heatindex);
    }

    // for each loop for calculation

}
}

0 个答案:

没有答案