首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将2D字符串数组的结果除以子数组的长度?

如何将2D字符串数组的结果除以子数组的长度?
EN

Stack Overflow用户
提问于 2021-08-16 06:57:03
回答 1查看 33关注 0票数 0

如何将二维数组的结果除以子数组的长度。在第三个问题上我需要帮助。Where要求输入缺少的代码行,以查找每个跑步者的平均时间。使用averageVal。

下面是问题“我们给了你一个名为averageVal的变量,它当前存储了0。编辑这行代码来找出每个跑步者的平均时间。”

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-16 09:22:15

我想这一行已经完成了任务

代码语言:javascript
复制
double averageTime = runnerTime / times[outer].length;

问题是当你用

代码语言:javascript
复制
System.out.println("Average of runner " + outer + ": " + averageVal + averageTime);

averageValaverageTime没有加在一起(我不明白你为什么要加它们)。它们是附加的。

例如:

代码语言:javascript
复制
    int one = 1;
    int three = 3;
    System.out.println("" + one + three);

输出是13,而不是4。

原因是,当您一起打印一个基元时,当字符串,基元将是整数的auto boxed和语句变成"".toString() + one.toString() + three.toString()。因为toString()返回字符串,所以运算符+会将它们链接在一起。

我整理了您的代码,并发布了下面的输出

代码语言:javascript
复制
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);

        }
    }
}

控制台输出

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68798570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档