求二维数组java的总和 [英] finding sum of two dimensional array java

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

问题描述

我正在一个项目中,我必须读取文件并将内容输入2D数组.然后,我必须对每一行,每一列以及矩阵的周长求和.到目前为止,除外围功能外,我一切正常.我正在尝试为两个外部列的顶行,底行和中间创建单独的for循环.

I am working on a project where I have to read a file and enter the content into a 2D array. Then I have to sum each row, each column, and the perimeter of the matrix. I have everything working so far except the perimeter. I am trying to create separate for loops for the top row, bottom row, and middle of the two outside columns.

矩阵文件如下:

1 2 3 4 
2 4 6 8 
2 4 6 8 
3 2 3 4 

因此,周长总计应为42. 现在,我可以成功地将第一行和最后一行添加为等于22.但是,当我将列添加到总数中时,我得到32.

Therefore the perimeter should add up to 42. Right now I can successfully add the first row and the last row to equal 22. However, when I add the columns to that total I get 32.

这是代码:

import java.util.*; // Scanner class
import java.io.*;  // File class

public class Lab10
{
   static public void main( String [ ] args )  throws Exception
   {    
  if ( args.length != 1 )
  {
    System.out.println("Error -- usage is:  java Lab10 matdataN.txt");
    System.exit( 0 );
  }

    //Requirement #1: first int value: # of rows, second int value: # of cols
    File newFile = new File(args[0]); 
    Scanner in = new Scanner(newFile);

    int numRows = in.nextInt();
    int numCols = in.nextInt();

    //Requirement #2: declare two-d array of ints
    int[][] matrix;
    matrix = new int[numRows][numCols];

    //Requirement #3 & 4: read file one line at a time (nested for loops 
        //and nextInt()) and print

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            matrix[i][j] = in.nextInt();
            System.out.print(matrix[i][j]+ " ");
        }
    System.out.println();
    }

    //Requirement #5: traverse each row and sum the values and display the sums
    int rowTotal = 0;
    for (int i = 0; i < numRows; i++)
    {
        rowTotal = 0;
        for (int j = 0; j < numCols; j++)
        {
            rowTotal += matrix[i][j];
        }
    System.out.println("Sum for row = " + rowTotal);
    }

    //Requirement #6: traverse each column and sum the values and display the sums
    int colTotal = 0;
    for (int i = 0; i < numRows; i++)
    {
        colTotal = 0;
        for (int j = 0; j < numCols; j++)
        {
            colTotal += matrix[j][i];
        }
    System.out.println("Sum for col = " + colTotal);
    }

    //Requirement #7: traverse the perimeter and sum the values and display the sum

    //sum bottom row matrix
    int perTotal = 0;
    for (int i = (numRows-1); i < numRows; i++)
    {
        perTotal = 0;
        for (int j = 0; j < numCols; j++)
        {
            perTotal += matrix[i][j];
        }
    }

    //sum + top row matrix
    for (int i = 0; i < numRows - (numRows-1); i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            perTotal += matrix[i][j];
        }
    System.out.println("Sum of perimeter = " + perTotal);
    }

    // sum + first col middle
    for (int i = 1; i < (numRows-1); i++)
    {
        for (int j = 0; j < numCols - (numCols-1); j++)
        {
            perTotal += matrix[j][i];
        }
    System.out.println("Sum = " + perTotal);
    }

    // sum + last col middle
    for (int i = 1; i < (numRows-1); i++)
    {
        for (int j = (numCols-1); j < numCols; j++)
        {
            perTotal += matrix[j][i];
        }
    System.out.println(perTotal);
    }

   }

如果有人能帮助我将第一列和最后一列的总和设为2 + 2和8 + 8,我将非常感激.或者,如果您有一种更好的方法来寻找周长.预先感谢!

I would be hugeeeeeely appreciative if anyone could help me total the middle of the first and last column (should be 2+2 and 8+8). Or if you have an altogether better way of finding the perimeter. Thanks in advance!

推荐答案

我建议您这样做:

int perTotal = 0;
// top and bottom row
for (int c = 0; c < numCols; c++)
    perTotal += matrix[0][c] + matrix[numRows-1][c];
// left and right column
for (int r = 1; r < numRows-1; r++)
    perTotal += matrix[r][0] + matrix[r][numCols-1];

// output
System.out.println("Perimeter=" + perTotal);

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

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