英文:
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 < testScores.length; i++) {
int sum = 0;
for (int j = 0; j < testScores[i].length; j++) {
sum += testScores[i][j];
}
System.out.println("Average of Test#" + testScores[i]
+ "is " + 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<Map.Entry<Integer,Double>>
.mapToObj(i -> 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<Map.Entry<Integer,Double>>
.mapToObj(i -> 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("Average of Test#" + testNumber
+ " is " + sum / testScores[i].length);
System.out.println();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论