[英]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();
}
}
我认为您的代码没有任何逻辑问题。 但是,保持代码整洁和用户友好同样重要。 如果您对编程很认真,我建议您解决以下几点:
以下声明是不必要的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.