如何将二维数组的结果除以子数组的长度。在第三个问题上我需要帮助。Where要求输入缺少的代码行,以查找每个跑步者的平均时间。使用averageVal。
下面是问题“我们给了你一个名为averageVal的变量,它当前存储了0。编辑这行代码来找出每个跑步者的平均时间。”
public class RowMajor {
public static void main(String[] args) {
// Given runner lap data
double[][] times = {{64.791, 75.972, 68.950, 79.039, 73.006, 74.157}, {67.768, 69.334, 70.450, 67.667, 75.686, 76.298}, {72.653, 77.649, 74.245, 62.121, 63.379, 79.354}};
// Replace the incorrect for loop headers, use the iterators 'outer' and 'inner' for the outer and inner loops
double runnerTime = 0.0;
for(int outer = 0; outer < times.length; outer++) {
runnerTime = 0.0;
for(int inner = 0; inner < times[outer].length; inner++) {
System.out.println("Runner index: " + outer + ", Time index: " + inner);
// Enter the missing line of code to sum up the values in each row. Use the variable runnerTime
runnerTime += times[outer][inner];
}
// I need help with this question below.
// Enter the missing line of code to find the average time of each runner. Use the variable averageVal
double averageVal = 0;
// Below is what I tried but it keep showing an error "The correct average time for runner 0 was not printed to the console."
double averageTime = runnerTime / times[outer].length;
System.out.println("Sum of runner " + outer + " times: " + runnerTime + averageTime);
System.out.println("Average of runner " + outer + ": " + averageVal + averageTime);
}
}
}
发布于 2021-08-16 01:22:15
我想这一行已经完成了任务
double averageTime = runnerTime / times[outer].length;
问题是当你用
System.out.println("Average of runner " + outer + ": " + averageVal + averageTime);
averageVal
和averageTime
没有加在一起(我不明白你为什么要加它们)。它们是附加的。
例如:
int one = 1;
int three = 3;
System.out.println("" + one + three);
输出是13,而不是4。
原因是,当您一起打印一个基元时,当字符串,基元将是整数的auto boxed和语句变成"".toString() + one.toString() + three.toString()
。因为toString()
返回字符串,所以运算符+
会将它们链接在一起。
我整理了您的代码,并发布了下面的输出
public class RowMajor {
public static void main(String[] args) {
// Given runner lap data
double[][] times = { { 64.791, 75.972, 68.950, 79.039, 73.006, 74.157 },
{ 67.768, 69.334, 70.450, 67.667, 75.686, 76.298 },
{ 72.653, 77.649, 74.245, 62.121, 63.379, 79.354 } };
// Replace the incorrect for loop headers, use the iterators 'outer' and 'inner'
// for the outer and inner loops
double runnerTime = 0.0;
for (int outer = 0; outer < times.length; outer++) {
runnerTime = 0.0;
for (int inner = 0; inner < times[outer].length; inner++) {
System.out.println("Runner index: " + outer + ", Time index: " + inner);
// Enter the missing line of code to sum up the values in each row. Use the
// variable runnerTime
runnerTime += times[outer][inner];
}
// I need help with this question below.
// Enter the missing line of code to find the average time of each runner. Use
// the variable averageVal
double averageVal = runnerTime / times[outer].length;
System.out.println("Sum of runner " + outer + " times: " + runnerTime);
System.out.println("Average of runner " + outer + ": " + averageVal);
}
}
}
控制台输出
Runner index: 0, Time index: 0
Runner index: 0, Time index: 1
Runner index: 0, Time index: 2
Runner index: 0, Time index: 3
Runner index: 0, Time index: 4
Runner index: 0, Time index: 5
Sum of runner 0 times: 435.9149999999999
Average of runner 0: 72.65249999999999
Runner index: 1, Time index: 0
Runner index: 1, Time index: 1
Runner index: 1, Time index: 2
Runner index: 1, Time index: 3
Runner index: 1, Time index: 4
Runner index: 1, Time index: 5
Sum of runner 1 times: 427.2030000000001
Average of runner 1: 71.20050000000002
Runner index: 2, Time index: 0
Runner index: 2, Time index: 1
Runner index: 2, Time index: 2
Runner index: 2, Time index: 3
Runner index: 2, Time index: 4
Runner index: 2, Time index: 5
Sum of runner 2 times: 429.401
Average of runner 2: 71.56683333333334
https://stackoverflow.com/questions/68798570
复制