>我正在尝试矩阵的行数之和 当我只是将元素放入 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];
}
}
}
}
使用您提供的示例输入,您不应该读取行数和列数,而应该只读取行数和列数的单个整数:
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;
您可以简单地使用
row
和column
,而不是为同一事物创建rows
和cols
。您应该删除所有在代码中产生噪音的不必要的东西。
-
您错过了打印每列的总和,即
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