繁体   English   中英

如何分别对二维数组的行中的值求和,并分别对列中的值求和? (爪哇)

[英]How do I sum the values in the row of a 2D array separately, and sum the values in the column separately? (Java)

我试图对二维数组行中的所有值求和,但我似乎无法弄清楚程序出了什么问题。

问题出在程序末尾的方法。 我只是想在这里总结行值。 我没有包括我对列求和的尝试,因为它也是错误的。

销售报表.txt

20
74
22
05
61
84
14
86
55
22
09
81
83
91
23
49
96 
45
41
45
26
77
77
19
84
03
65
91
31
62
73
92
49
93
43
36
69
05
96
54
59
84
14
16
49
35
28
51
60
98
42
61
59
40
01
98
85
80
90
20
22
43
54
71
47
35
81
86
04
61
60
34
35
08
14
44
90
51
27
50
73
53
75
52
19
72
86
41
17
04
46
11
45
25
21
66
96
48
98
06
61
38
11
96
19
63
32
03
11
30
84
80
17
73
90
40
69
55
90
92

我的代码

import java.util.Scanner; // Needed for the Scanner class
import java.io.File; // Needed for the File class
import java.io.IOException;

public class salesReport
{
   public static void main(String[] args) throws IOException
   {
      // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
      
      // Declaration of variables
      int C,D,E;
      int[] A;
      int[][] B;
      int rows = 10;
      int columns = 12;

      // Method to read file
      A = arrayFile(rows * columns);
    
      // Declaration of 2D array
      B = new int[rows][columns];
   
      for(int r=0; r < rows; r++)
      {
         for(int c = 0;c < columns;c++)
         {
            B[r][c] = A[r*c+c];
         }
      }
    
      // Method to show array
      showArray(B);

      // Method to sum entire 2D array
      C = totalSales(B);
      System.out.println("Total sales for the year: $" + C);
      System.out.println();
      
      // Method to average the sum of the 2D array
      D = averagesSales(C);
      System.out.println("Average sales for the year: $" + D);
      System.out.println();
      
      // Method to sum value in the ROW ONLY
      E = totalDivisionSales(B);
   
   }
   
   // Method to read file
   public static int[] arrayFile(int a) throws IOException
   {
        // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
   
      int A[];
      A = new int[a];
   
      System.out.print("Enter the filename: ");
      String filename = keyboard.nextLine();
      keyboard.close();
   
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
   
      int i = 0;
   
      while (inputFile.hasNextLine())
      {
         A[i] = inputFile.nextInt();
         i++;
      }
      inputFile.close();
   
      return A;
   }
   
   // Method to show array
   public static void showArray(int[][] B)
   {
      for(int r = 0;r < B.length;r++)
      {
         for(int c = 0;c < B[r].length;c++)
         {
            System.out.print(B[r][c] + "   ");
         }
         System.out.print("\n\n\n");
      }
    
   }
   
   // Method to sum entire 2D array
   private static int totalSales(int[][] B)
   {
      int total = 0;
   
      for (int r = 0;r < B.length;r++)
      {
         for (int c = 0;c < B[r].length;c++)
         {
            total += B[r][c];
         }
      }
      return total;
   }

   // Method to average the sum of the 2D array
   public static int averagesSales(int C)
   {
      int avg;
    
      avg = C / 120;
      return avg;
   }
   
   // Method to sum value in the ROW ONLY
   public static int totalDivisionSales(int[] B)
   {
    int sum=0;
    
      for(int r = 0;r < B.length;r++)
      {
       sum = sum + B[r];
      }
      return total;
   }   
}

如果不对您在做什么进行任何解释,您似乎很难对这些行求和。 但我认为您的问题是错误地构建了二维数组。 我相信它应该是这样的:

for(int r=0; r < rows; r++) {
     for(int c = 0;c < columns;c++) {
                B[r][c] = A[r*12+c]; // <-- notice the 12
     }
}

话说回来

  • 您可以在读入行时轻松地对行求和并同时保存
  • 然后你可以做列总和

int[][] rows = new int[10][];
int[] rowSums = new int[10];
for (int row = 0; row < rows.length; row++){
    int[] r = new int[12];
    for (int col = 0; col < r.length; col++) {
        int val = scanner.nextInt();
        rowSums[row] += val;  // sum rows
        r[col] = val;         // save value
    }
    rows[row] = r;
}


int rowNo = 1;
for(int sum : rowSums) {
    System.out.printf("row %2d = %d%n", rowNo++, sum);
}
  • 要对列求和,外循环应遍历它们。
  • 然后内部循环遍历当前列的行。
int [] colSums = new int[12];
for (int col = 0; col < 12; col++) {
    for (int row = 0; row < rows.length; row++) {
        colSums[col] += rows[row][col];
    }
}
System.out.println();
int colNo = 1;
for (int sum : colSums) {
    System.out.printf("col %2d = %d%n", colNo++, sum);
}

印刷

row  1 = 533
row  2 = 672
row  3 = 722
row  4 = 560
row  5 = 734
row  6 = 598
row  7 = 572
row  8 = 453
row  9 = 571
row 10 = 731

col  1 = 499
col  2 = 472
col  3 = 584
col  4 = 502
col  5 = 538
col  6 = 516
col  7 = 398
col  8 = 620
col  9 = 474
col 10 = 564
col 11 = 525
col 12 = 454

笔记

这可能是一个问题,因为int除法会丢弃分数。 您可能希望将int声明更改为double

   // Method to average the sum of the 2D array
   public static int averagesSales(int C)
   {
      int avg;
    
      avg = C / 120;
      return avg;
   }

你的totalDivisionSales方法中的total也应该是sum

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM