2015-12-02 207 views
1

我想創建另一個數組,但我希望它是數組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]); 
    } 

} 
} 
+3

您需要循環通過量h你的'gainT'數組,檢查每個索引,進行數學計算,並將結果存儲到'gainTC'索引中。 – CubeJockey

+0

我該怎麼做呢?我對編程還很陌生,上週剛剛學會了循環。你能告訴我一個簡單的例子嗎? – Skateabuga

+0

當然,我發佈了一個簡單的例子。您可以填寫空格:) – CubeJockey

回答

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循環: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

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

+0

非常感謝! – Skateabuga

+0

不客氣@Skateabuga,祝您好運! – CubeJockey