我是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]);
}
}
}
答案 0 :(得分:0)
我猜你错过了“第7天 - 第5小时”的含义作为两个数字的减法,但是imho只是意味着在第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];
}
答案 1 :(得分:0)
我不确定你为什么期望第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