如何更改二维数组中的所有值?

How to change all values in 2d array?

我是 Java 编程 class 的学生,我的问题涉及将二维数组中的所有温度从华氏度转换为摄氏度。该数组包含一个月中一天中每个小时的所有温度。之后讲师给了我们一个打印语句,看看数组是否转换了,而我的没有。 getTemperature 方法只是指出数组中的特定温度,在打印语句中使用时应该已将其转换为摄氏度。我试着这样做。

public static void main (String[] args) {
    // [day][hour]
    double[][] temps = {
        {30.9, 30.9, 30.9, 33.1, 36.0, 36.0, 37.4, 39.0, 43.0, 48.2, 51.1, 55.9, 60.1, 60.1, 60.1, 60.1, 57.0, 54.0, 53.1, 55.0, 57.0, 59.0, 57.9, 57.0},
        {57.0, 59.0, 59.0, 60.1, 61.0, 63.0, 63.0, 63.0, 63.0, 62.1, 61.0, 61.0, 60.1, 59.0, 57.9, 57.0, 55.4, 52.0, 48.9, 46.0, 44.1, 42.1, 39.0, 36.0},
        {36.0, 34.0, 32.0, 30.9, 28.9, 28.0, 26.1, 30.0, 33.8, 35.1, 37.9, 41.0, 43.0, 44.6, 43.0, 43.0, 41.0, 39.0, 36.0, 34.0, 32.0, 32.0, 28.9, 28.9},
        {30.0, 28.9, 26.6, 27.0, 28.0, 28.0, 30.9, 41.0, 48.9, 51.1, 52.0, 53.1, 54.0, 55.0, 55.0, 54.0, 53.1, 51.1, 51.1, 50.0, 48.9, 50.0, 51.8, 51.8},
        {51.1, 48.9, 48.9, 46.9, 46.0, 46.0, 45.0, 45.0, 46.0, 46.0, 46.9, 48.9, 53.1, 55.0, 55.0, 54.0, 52.0, 46.9, 44.1, 41.0, 37.9, 36.0, 33.1, 32.0},
        {30.0, 28.0, 27.0, 25.0, 23.0, 21.9, 24.1, 26.1, 28.0, 30.9, 34.0, 37.0, 39.2, 42.1, 43.0, 43.0, 43.0, 39.9, 39.0, 39.0, 37.9, 37.9, 37.0, 35.1},
        {32.0, 34.0, 34.0, 33.1, 33.1, 32.0, 32.0, 33.1, 42.1, 46.9, 52.0, 55.0, 57.0, 59.0, 57.9, 57.0, 53.1, 50.0, 48.9, 48.9, 48.9, 48.0, 48.9, 48.9},
        {48.0, 48.9, 48.9, 48.2, 48.9, 50.0, 48.9, 51.1, 55.9, 60.8, 64.0, 66.9, 69.1, 69.1, 66.9, 68.0, 64.9, 60.1, 57.0, 55.9, 57.0, 55.9, 55.0, 55.9},
        {55.0, 54.0, 54.0, 53.6, 53.1, 51.1, 51.1, 51.1, 57.9, 61.0, 66.9, 69.1, 71.1, 71.1, 70.0, 66.9, 64.0, 60.1, 57.9, 55.9, 57.0, 55.0, 54.0, 52.0},
        {50.0, 52.0, 52.0, 50.0, 48.9, 46.0, 44.1, 43.0, 41.0, 41.0, 39.0, 39.0, 39.9, 39.9, 39.0, 39.9, 39.0, 37.9, 37.9, 39.0, 39.0, 39.0, 39.0, 39.2},
        {37.9, 37.9, 37.0, 36.0, 36.0, 36.0, 36.0, 36.0, 37.0, 37.0, 37.9, 39.0, 41.0, 43.0, 46.9, 48.2, 48.9, 44.1, 39.9, 37.0, 36.0, 36.0, 34.0, 33.1},
        {32.0, 32.0, 32.0, 33.8, 34.0, 33.1, 33.1, 42.1, 52.0, 57.0, 61.0, 63.0, 64.0, 64.9, 64.0, 64.0, 64.0, 55.9, 46.9, 44.1, 42.8, 39.0, 37.0, 34.0},
        {33.1, 30.9, 30.0, 28.9, 28.0, 28.0, 28.0, 28.9, 30.9, 30.9, 33.1, 36.0, 36.0, 37.0, 39.0, 39.2, 37.9, 35.1, 30.0, 30.9, 28.9, 25.0, 24.1, 24.8},
        {24.1, 23.0, 23.0, 21.9, 23.0, 21.0, 23.0, 26.1, 37.0, 43.0, 46.9, 50.0, 51.1, 51.1, 51.1, 52.0, 51.1, 48.9, 48.0, 48.9, 50.0, 51.1, 51.1, 51.1},
        {50.0, 39.0, 33.1, 30.2, 27.0, 25.0, 23.0, 23.0, 24.1, 26.1, 27.0, 28.9, 30.9, 33.1, 34.0, 34.0, 34.0, 30.9, 30.0, 28.0, 26.1, 26.1, 24.1, 23.0},
        {23.0, 23.0, 23.0, 24.1, 25.0, 24.1, 23.0, 24.1, 26.1, 27.0, 28.9, 32.0, 34.0, 33.1, 33.1, 33.1, 32.0, 33.1, 37.0, 39.0, 42.1, 44.6, 48.2, 48.2},
        {39.9, 36.0, 36.0, 60.8, 52.0, 35.1, 36.0, 33.1, 30.0, 30.0, 30.0, 32.0, 32.0, 32.0, 33.1, 32.0, 32.0, 30.9, 30.9, 30.9, 30.0, 30.0, 30.0, 28.9},
        {28.9, 27.0, 27.0, 27.0, 27.0, 28.0, 27.0, 28.9, 33.1, 36.0, 39.0, 42.1, 44.1, 46.9, 48.0, 46.9, 45.0, 43.0, 41.0, 42.1, 39.0, 37.0, 36.0, 32.0},
        {30.9, 30.0, 28.0, 26.1, 24.1, 23.0, 21.0, 21.9, 23.0, 24.1, 25.0, 26.6, 28.0, 28.9, 30.0, 28.9, 28.0, 26.1, 25.0, 21.9, 19.9, 21.0, 19.0, 19.0},
        {19.0, 18.0, 17.1, 15.8, 15.1, 14.0, 12.9, 14.0, 17.1, 19.9, 23.0, 26.1, 28.0, 30.0, 30.9, 32.0, 30.9, 28.0, 25.0, 21.9, 21.9, 21.9, 21.0, 19.9},
        {19.9, 19.9, 21.0, 19.9, 19.9, 19.9, 21.9, 27.0, 37.0, 45.0, 48.0, 50.0, 53.1, 55.9, 57.9, 57.0, 57.0, 55.0, 53.1, 54.0, 55.9, 55.0, 55.4, 55.9},
        {55.9, 55.9, 55.9, 55.4, 55.9, 55.0, 48.9, 48.0, 59.0, 61.0, 64.0, 64.9, 66.9, 66.0, 66.9, 66.0, 64.9, 63.0, 62.1, 59.0, 57.0, 57.9, 57.0, 55.9},
        {55.0, 55.9, 55.0, 55.4, 55.0, 54.0, 57.2, 55.9, 57.0, 55.9, 46.9, 46.0, 44.1, 42.8, 41.0, 39.9, 39.0, 37.9, 37.0, 37.0, 35.1, 33.1, 32.0, 30.9},
        {30.9, 30.9, 30.9, 30.0, 30.0, 28.9, 28.0, 26.1, 27.0, 28.0, 28.0, 28.0, 28.0, 28.9, 30.0, 30.0, 30.0, 30.9, 30.9, 30.0, 30.0, 30.0, 30.0, 30.0},
        {30.0, 30.0, 30.0, 30.0, 30.2, 30.0, 30.0, 30.9, 30.9, 33.8, 33.1, 34.0, 36.0, 37.9, 39.9, 39.2, 39.0, 39.0, 37.9, 37.9, 36.0, 36.0, 36.0, 35.1},
        {35.1, 36.0, 37.9, 37.0, 37.9, 39.2, 37.0, 35.1, 35.1, 36.0, 37.9, 37.4, 39.0, 39.0, 39.2, 39.0, 39.0, 37.9, 37.9, 37.0, 37.0, 37.0, 37.0, 36.0},
        {36.0, 35.1, 34.0, 34.0, 33.1, 33.8, 32.0, 32.0, 37.4, 39.9, 45.0, 46.0, 48.0, 48.9, 50.0, 51.1, 46.9, 45.0, 41.0, 37.9, 39.0, 37.0, 36.0, 37.0},
        {35.1, 33.1, 32.0, 30.9, 30.0, 30.0, 30.0, 30.0, 33.1, 37.0, 41.0, 45.0, 46.9, 48.9, 50.0, 46.9, 45.0, 43.0, 42.1, 39.9, 39.9, 39.0, 37.9, 37.9}
    };
    convertToCelsius(temps);
    System.out.println("Day 7 - Hour 5: " + getTemperature(temps, 7, 5)); // Should be 10.0 (starting at 0)
}
    public static double getTemperature (double[][] temps, int day, int hour) {
    double temp = 0;
    double temp2 = 0;
    for (int row = 0; row < temps.length; row++) {
        for (int col = 0; col < temps[row].length; col++) {
            temp = row;
            temp2 = col;
        }
    }
    return temp + temp2;
}

public static double c2f (double c) {
    double temp = (c * 9.0/5) + 32.0;
    return temp;
}

public static void convertToCelsius(double[][] temps) {
    for (int row = 0; row < temps.length; row++) {
        for (int col = 0; col < temps[row].length; col++) {
            temps[row][col] = c2f(temps[row][col]);
        }
    }
}

我猜你误解了 "day 7 - hour 5" 的意思是两个数字的减法,但恕我直言,意思是获取第 7 天第 5 小时的温度。所以 getTemperature 方法应该看起来像这样

//Assumes that day and hour counting start at 0
    public static double getTemperature (double[][] temps, int day, int hour) {
        return temps[day][hour];
    }

//Assumes that day and hour counting start at 1
    public static double getTemperature (double[][] temps, int day, int hour) {
        return temps[day-1][hour-1];
    }

我不确定为什么您预计第 7 天 - 第 5 小时的温度为 10 度。celcious 的温度为 33.1 度,应转换为 91.58 华氏度。

您的代码可以正确转换温度。只是你的getTemperature (double[][] temps, int day, int hour)没有意义。

将该方法更改为

public static double getTemperature (double[][] temps, int day, int hour){
    return temps[day-1][hour-1];
}

现在调用这个: System.out.println("Day 7 - Hour 5: " + getTemperature(temps, 7, 5)); returns 91.58