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

标签 java arrays

我正在尝试创建另一个数组,但我希望它是数组 double gainT[],它是华氏温度,转换为摄氏温度以生成另一个名为 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]);
    }

 }
 }

最佳答案

这是一个不完整的例子,您可以自己填空:

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

关于java - 将数组中的数字转换为另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050973/

相关文章:

javascript - 在 Jison 中制作数组

java - 如何比较不同类中的两个数组?

java - 如何以编程方式禁用屏幕锁定密码。

java - 如果发生无限递归,如何让递归函数返回 false

java - long 和 double 赋值不是原子的——这有什么关系?

java - 使用 Selenium 和 java 查找 href 链接

java - 如何在同一个 bundle (模块)内的另一个服务中使用服务?

c - 将保存未知长度的字符串并能够稍后删除它们的数据结构

javascript - react : Are keys useful for any situations other than lists?

java - if 语句检查变量是否在数组中?