提问者:小点点

使用扫描仪在二维数组中插入元素时出现逻辑错误


当我把元素放入2D数组时,我试图求矩阵行的和,输出是正确的,但当我尝试使用扫描仪时,输出结果是不同的

样本输入

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];    
            }           
        }
    }
} 

共2个答案

匿名用户

使用您提供的示例输入,您不应该读取行数和列数,而应该只读取一个用于行数和列数的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(); 
    }
}

匿名用户

我看不出您的代码有任何逻辑问题。 然而,保持代码干净和用户友好同样重要。 如果您对编程很认真,我建议您注意以下几点:

>

  • 以下声明是不必要的:

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

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

    您应该删除所有这些不必要的东西,这些东西会在代码中产生噪音。

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

    System.out.println(sumCol);
    

    您不需要用main声明此代码的throws IOException

    您应该始终显示一条描述输入的消息; 否则,用户仍然不知道他/她应该输入什么。

    以下是纳入这些评论的守则:

    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