将数组中的数字转换为另一个数组

时间:2015-12-02 19:08:40

标签: java arrays

我试图创建另一个数组,但我希望它是数组double gainT [],这是华氏温度,转换为Celcius制作另一个名为double gainTC []的数组。我也试图做同样的事情,但降水量。我似乎无法找到办法做到这一点。

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

 public class AnnualClimate1 {
 public static void main(String [] args) throws IOException
{
    Scanner in = new Scanner(System.in);

    System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
    String tFC = in.nextLine();

    System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
    String pIC = in.nextLine();

    System.out.println("");
    System.out.println("");

    System.out.println("                 Climate Data");
    System.out.println("        Location: Gainesville, Florida");
    System.out.println("               Temperature " + tFC + "     Precipitation " + pIC);
    System.out.println("=================================================");

    double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};

    double gainTC[] = {(gainT[] - 32) / 1.8};

    double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};

    double gainPC[] = {gainP[] / .3937};

    if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
        System.out.println("Jan.                " + gainT[1] + "               " + gainP[1]);
        System.out.println("Feb.                " + gainT[2] + "               " + gainP[2]);
        System.out.println("Mar.                " + gainT[3] + "               " + gainP[3]);
        System.out.println("Apr.                " + gainT[4] + "               " + gainP[4]);
        System.out.println("May                 " + gainT[5] + "               " + gainP[5]);
        System.out.println("Jun.                " + gainT[6] + "               " + gainP[6]);
        System.out.println("Jul.                " + gainT[7] + "               " + gainP[7]);
        System.out.println("Aug.                " + gainT[8] + "               " + gainP[8]);
        System.out.println("Sep.                " + gainT[9] + "               " + gainP[9]);
        System.out.println("Oct.                " + gainT[10] + "               " + gainP[10]);
        System.out.println("Nov.                " + gainT[11] + "               " + gainP[11]);
        System.out.println("Dec.                " + gainT[12] + "               " + gainP[12]);
    }
    else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
        System.out.println("Jan.                " + gainTC[1] + "               " + gainPC[1]);
        System.out.println("Feb.                " + gainTC[2] + "               " + gainPC[2]);
        System.out.println("Mar.                " + gainTC[3] + "               " + gainPC[3]);
        System.out.println("Apr.                " + gainTC[4] + "               " + gainPC[4]);
        System.out.println("May                 " + gainTC[5] + "               " + gainPC[5]);
        System.out.println("Jun.                " + gainTC[6] + "               " + gainPC[6]);
        System.out.println("Jul.                " + gainTC[7] + "               " + gainPC[7]);
        System.out.println("Aug.                " + gainTC[8] + "               " + gainPC[8]);
        System.out.println("Sep.                " + gainTC[9] + "               " + gainPC[9]);
        System.out.println("Oct.                " + gainTC[10] + "               " + gainPC[10]);
        System.out.println("Nov.                " + gainTC[11] + "               " + gainPC[11]);
        System.out.println("Dec.                " + gainTC[12] + "               " + gainPC[12]);
    }

 }
 }

1 个答案:

答案 0 :(得分:3)

这是一个不完整的示例,因此您可以自己填写空白:

double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};

double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array

//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
     double math = //use your conversion math here, and store the value
     gainTC[i] = math; //assign the results of your math to the same spot in the new array
}

如果您想了解有关循环和数组的更多信息,请点击此处:

For Loops:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

阵列:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html