簡體   English   中英

使用掃描儀將元素插入二維數組時出現邏輯錯誤

[英]Logical error while inserting elements in to Two dimensional array using Scanner

我正在嘗試對矩陣行求和當我只是將元素放入二維數組 output 是正確的但是當我嘗試使用掃描儀 output 結果是不同的

樣本輸入

2
1 2 
3 4

Output:

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

使用您提供的示例輸入,您不應該讀取行數和列數,而只是讀取行數和列數的單個 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(); 
    }
}

我認為您的代碼沒有任何邏輯問題。 但是,保持代碼整潔和用戶友好同樣重要。 如果您對編程很認真,我建議您解決以下幾點:

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

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

    您可以簡單地使用rowcolumn ,而不是為同一事物創建rowscols

    您應該刪除所有在您的代碼中產生噪音的不必要的東西。

  2. 您錯過了打印每列的總和,即

    System.out.println(sumCol);
  3. 對於此代碼,您不需要使用main聲明throws IOException

  4. 您應該始終顯示描述輸入的消息; 否則,用戶對他/她應該輸入什么一無所知。

    下面給出了包含這些注釋的代碼:

     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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM