打印递增的行索引和二维数组中的平均值

huangapple go评论70阅读模式
英文:

Printing an incremented row index and average value in a 2D array

问题

int[][] testScores = {
        {100, 85, 91, 75, 82}, 
        {81, 75, 68, 92, 87}, 
        {99, 71, 75, 84, 91}, 
        {97, 91, 68, 72, 83}};

for (int i = 0; i < testScores.length; i++) {
    int sum = 0;
    for (int j = 0; j < testScores[i].length; j++) {
        sum += testScores[i][j];
    }

    int testNumber = i + 1;
    System.out.println("Test#" + testNumber + " has an average of " + sum / testScores[i].length);
    System.out.println();
}

Final output should be:

Test#1 has an average of 86
Test#2 has an average of 80
Test#3 has an average of 84
Test#4 has an average of 82

英文:

I have a 2D array of 4 tests, 5 scores a piece. I need to print the average out with which test number it belongs to. I also need the tests numbers to be 1-4 instead of 0-3.

I'm unsure of how to renumber the tests and get it to print a readable number:

int[][] testScores = {
        {100, 85, 91, 75, 82}, 
        {81, 75, 68, 92, 87}, 
        {99, 71, 75, 84, 91}, 
        {97, 91, 68, 72, 83}};

for (int i = 0; i &lt; testScores.length; i++) {
    int sum = 0;
    for (int j = 0; j &lt; testScores[i].length; j++) {
        sum += testScores[i][j];
    }

    System.out.println(&quot;Average of Test#&quot; + testScores[i]
            + &quot;is &quot; + sum / testScores[i].length);
    System.out.println();
}

My final output should be something like:

Test#I has an average of J where I begins at 1 and goes to 4.

I will most likely need to add more tests later on as this develops, but I'm sure I will be able to manage that once I can get this sorted. I appreciate the assistance in advance!

答案1

得分: 1

你可以遍历这个数组的行索引的 range,对于每一行,将行的递增索引和其 average 值组合在一个 entry 中。你的代码可能类似于以下内容:

int[][] testScores = {
        {100, 85, 91, 75, 82},
        {81, 75, 68, 92, 87},
        {99, 71, 75, 84, 91},
        {97, 91, 68, 72, 83}};

IntStream.range(0, testScores.length)
        // Stream&lt;Map.Entry&lt;Integer,Double&gt;&gt;
        .mapToObj(i -&gt; Map.entry(
                // 分数行号
                i + 1,
                // 本行平均值
                Arrays.stream(testScores[i])
                        .average().orElse(0)))
        // 输出
        .forEach(System.out::println);
1=86.6
2=80.6
3=84.0
4=82.2
英文:

You can iterate over the range of row indices of this array, and for each row, put together in one entry the incremented index of the row and its average value. Your code might look something like this:

int[][] testScores = {
        {100, 85, 91, 75, 82},
        {81, 75, 68, 92, 87},
        {99, 71, 75, 84, 91},
        {97, 91, 68, 72, 83}};

IntStream.range(0, testScores.length)
        // Stream&lt;Map.Entry&lt;Integer,Double&gt;&gt;
        .mapToObj(i -&gt; Map.entry(
                // number of the line of scores
                i + 1,
                // average value for this line
                Arrays.stream(testScores[i])
                        .average().orElse(0)))
        // output
        .forEach(System.out::println);
1=86.6
2=80.6
3=84.0
4=82.2

答案2

得分: 0

根据Nick的评论,将testScores[i]更改为i,并且相应地按照测试编号递增testScores应该这样做:

int testNumber = i + 1;
System.out.println("测试#" + testNumber
        + "的平均分是" + sum / testScores[i].length);
System.out.println();
英文:

Per Nick's comment, testScores[i] needs changed to i and to increment the testScores accordingly by test number should be done this way:

int testNumber = i + 1;
System.out.println(&quot;Average of Test#&quot; + testNumber
        + &quot; is &quot; + sum / testScores[i].length);
System.out.println();

huangapple
  • 本文由 发表于 2020年8月28日 10:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63626705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定