【问题标题】:Logical error while inserting elements in to Two dimensional array using Scanner使用扫描仪将元素插入二维数组时出现逻辑错误
【发布时间】:2020-10-25 00:03:40
【问题描述】:

我正在尝试对矩阵的行求和 当我将元素放入二维数组时输出是正确的,但是当我尝试使用扫描仪时输出结果不同

示例输入

2
1 2 
3 4

输出:

3
7

以下代码结果正确

import java.io.*;
import java.util.*;

public class matrix {
    public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);

 int a[][] = {       
                        {1, 2,},    
                           
                        { 3, 4}    
                    };    

int rows, cols, sumRow, sumCol;    
            
        //Initialize matrix a  
              
          //Calculates number of rows and columns present in given matrix    
          rows = a.length;    
        cols = a[0].length;    
            
        //Calculates sum of each row of given matrix    
        for(int i = 0; i < rows; i++){    
            sumRow = 0;    
            for(int j = 0; j < cols; j++){    
              sumRow = sumRow + a[i][j];    
            }    
            System.out.println(sumRow);    
        }    
            
        //Calculates sum of each column of given matrix    
        for(int i = 0; i < cols; i++){    
            sumCol = 0;    
            for(int j = 0; j < rows; j++){    
              sumCol = sumCol + a[j][i];    
            }
        }
    }
}

如果我尝试使用扫描仪,结果不正确

import java.io.*;
import java.util.*;

public class matrix {
    public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);

int row = sc.nextInt();

int column = sc.nextInt();

int [][] a = new int[row][column];
for (int i = 0; i < row; i++)
{
    for(int j = 0; j < column; j++) {
       
    a[i][j] = sc.nextInt(); 
    }
}

int rows, cols, sumRow, sumCol;    
            
        //Initialize matrix a  
              
          //Calculates number of rows and columns present in given matrix    
          rows = a.length;    
        cols = a[0].length;    
            
        //Calculates sum of each row of given matrix    
        for(int i = 0; i < rows; i++){    
            sumRow = 0;    
            for(int j = 0; j < cols; j++){    
              sumRow = sumRow + a[i][j];    
            }    
            System.out.println(sumRow);    
        }    
            
        //Calculates sum of each column of given matrix    
        for(int i = 0; i < cols; i++){    
            sumCol = 0;    
            for(int j = 0; j < rows; j++){    
              sumCol = sumCol + a[j][i];    
            }           
        }
    }
} 

【问题讨论】:

  • 我无法重现您的问题(link) 显示一些给出错误答案的示例。您的示例输入不正确,您没有输入列大小

标签: java multidimensional-array java.util.scanner


【解决方案1】:

使用您提供的示例输入,您不应该读取行数和列数,而只是读取行数和列数的单个 int:

int size = sc.nextInt();

int [][] a = new int[size][size];
for (int i = 0; i < size; i++) {
    for(int j = 0; j < size; j++) {       
        a[i][j] = sc.nextInt(); 
    }
}

【讨论】:

  • 但是如果我们有 2x3 矩阵呢?顺便说一句,谢谢您的答案适用于相同大小的矩阵
  • @User75432132 示例输入似乎不允许 2x3 矩阵。您能否提供示例输入来说明您希望如何接收这样的矩阵?
【解决方案2】:

我认为您的代码没有任何逻辑问题。但是,保持代码整洁和用户友好同样重要。如果您对编程很认真,我建议您解决以下几点:

  1. 以下声明是不必要的:

    rows = a.length;
    cols = a[0].length;
    

    您可以简单地使用rowcolumn,而不是为同一事物创建rowscols

    你应该删除所有在你的代码中产生噪音的不必要的东西。

  2. 您错过了打印每列的总和,即

    System.out.println(sumCol);
    
  3. 您不需要为此代码声明throws IOExceptionmain

  4. 您应该始终显示一条描述输入的消息;否则,用户对他/她应该输入什么一无所知。

    下面是包含这些 cmets 的代码:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            System.out.print("Enter the number of rows: ");
            int row = sc.nextInt();
            System.out.print("Enter the number of columns: ");
            int column = sc.nextInt();
    
            int[][] a = new int[row][column];
            for (int i = 0; i < row; i++) {
                System.out.println("Enter " + column + " integers: ");
                for (int j = 0; j < column; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
    
            int sumRow, sumCol;
    
            // Calculates sum of each row of given matrix
            for (int i = 0; i < row; i++) {
                sumRow = 0;
                for (int j = 0; j < column; j++) {
                    sumRow = sumRow + a[i][j];
                }
                System.out.println("Sum of row " + i + ": " + sumRow);
            }
    
            // Calculates sum of each column of given matrix
            for (int i = 0; i < column; i++) {
                sumCol = 0;
                for (int j = 0; j < row; j++) {
                    sumCol = sumCol + a[j][i];
                }
                System.out.println("Sum of column " + i + ": " + sumCol);
            }
        }
    }
    

    示例运行:

    Enter the number of rows: 3
    Enter the number of columns: 4
    Enter 4 integers: 
    1 9 2 8
    Enter 4 integers: 
    2 8 3 7
    Enter 4 integers: 
    3 7 4 6
    Sum of row 0: 20
    Sum of row 1: 20
    Sum of row 2: 20
    Sum of column 0: 6
    Sum of column 1: 24
    Sum of column 2: 9
    Sum of column 3: 21
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多