[英]How to change all values in 2d array?
我是一名Java编程课程的学生,我的问题涉及将2d数组中的所有温度从华氏温度转换为摄氏温度。 阵列具有一个月中一天中每个小时的所有温度。 之后,讲师给了我们一个打印语句,以查看数组是否已转换而我的未转换。 getTemperature方法仅指出数组中的特定温度,并且在print语句中使用时应已将其转换为摄氏度。 我尝试这样做。
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]);
}
}
}
我想您可能误解了“第7天-第5小时”的含义,因为它们是两个数字的减法,但是恕我直言只是意味着要在第5天的第7天获得温度。因此,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。摄氏度中的温度为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));
返回91.58
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.