在每行之后打印二维数组中的行的总和 [英] Print the sum of the row in a 2D Array after each row

查看:34
本文介绍了在每行之后打印二维数组中的行的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在该行之后打印出每行的总和.它需要像这样格式化

I'm trying to get the sum of each row printed out after that row. It needs to be formatted like this

2 5 6 3| 16
9 4 4 7| 24
1 10 2 3| 16
8 4 5 3| 20
-----------------
20 23 17 16| 76

不必那么好,但要接近它.我必须使用 toString 方法,这是我到目前为止所拥有的.感谢@KevinAnderson 解决了这个问题.需要将 rowSum[r] 附加到数组,而不是尝试使用另一个 for 循环来获取值.

Doesn't have to be as nice, but close to it. I have to use a toString method, here is what I have so far. Got it solved thanks to @KevinAnderson. Needed to append rowSum[r] to Arrays instead of trying to use another for loop to get the values.

public String toString() {
    //Stores the colSum into a string col
    String col = "";
    for (int j = 0; j < cell[0].length; j++) {
        col += " " + colSum[j];

    }
    //Calculates the total of both the rows and columns
    for (int i = 0; i < cell.length; i++) {
        for (int j = 0; j < cell[0].length; j++) {
            grandTotal += cell[i][j];
        }
    }
    //Prints the array
    String array = "";
    for (int r = 0; r < cell.length; r++) {
        for (int c = 0; c < cell[r].length; c++) {
            array += " " + cell[r][c];
        }
        array += "|" + +rowSum[r] + "\n";
    }
    return array + "-----------------" + "\n" + col + "| " + grandTotal;
}

rowSumcolSum 以单独的方法计算,如果需要,可以提供该方法,但它们确实按预期工作.

rowSum and colSum are calculated in a separate method that can be provided if need be, but they do work as intended.

我认为我需要做的是将 rowSum 的值存储在一个 int 值中并打印它,或者以某种方式通过 rowSum 递增.我已经尝试了这两种方法,到目前为止它会打印出存储在其中的整个数组.

What I believe I need to do is store the value of rowSum in either an int value and print it, or increment through rowSum in some way. I've tried both, and so far it prints out the whole array that it is stored into.

推荐答案

这里是使用两个 for 循环的解决方案.一个打印行的总和另一个打印列的总和还要构建整个总和,请在第一个或第二个 for 循环中对其进行跟踪.

Here a solution by using two for-loops. One to print the sum of the rows the other to print the sum of the columns Also to build the whole sum get track of it either in the first or the second for-loop.

package so;

import java.util.*;

public class RemoveString {
    public static void main(String[] args) {
        int[][] arr = {{2, 5, 6, 3}, {9, 4, 4, 7}, {1, 10, 2, 3}, {8, 4, 5, 3}};
        System.out.println(toString(arr));
    }

    public static String toString(int[][] cell) {
        int sum = 0;
        int allSum = 0;
        int counter = 0;
        String resultString = "";

        for (int i = 0; i < cell.length; i++) {
            for (int j = 0; j < cell[i].length; j++) {
                // Count the columns for the condition later on
                if (i == 0) {
                    counter++;
                }
                sum += cell[i][j];
                resultString += cell[i][j] + " ";
            }
            resultString += "| " + sum + "\n";
            sum = 0;
        }

        resultString += "--------------\n";

        for (int i = 0; i < counter; i++) {
            for (int j = 0; j < cell.length; j++) {
                sum += cell[j][i];
            }
            allSum += sum;
            resultString += sum + " ";
            sum = 0;
        }

        resultString += "|" + allSum;
        return resultString;
    }
}

创建输出

2 5 6 3 | 16
9 4 4 7 | 24
1 10 2 3 | 16
8 4 5 3 | 20
--------------
20 23 17 16 |76

这篇关于在每行之后打印二维数组中的行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆