java多维数组行和列和 publicstaticvoidmain(字符串[]args){ int行,cols,j,i; 扫描仪sc=新的扫描仪(System.in); System.out.println(“分别输入行数和列数”); 行=sc.nextInt(); cols=sc.nextInt(); int[][]矩阵=新的int[行+1][cols+1]; System.out.println(“输入元素”); 对于(i=0;i
上面的代码运行良好,但我的问题是:使用此逻辑,如果行数和列数不相同,我如何找到矩阵的java多维数组行和列和 publicstaticvoidmain(字符串[]args){ int行,cols,j,i; 扫描仪sc=新的扫描仪(System.in); System.out.println(“分别输入行数和列数”); 行=sc.nextInt(); cols=sc.nextInt(); int[][]矩阵=新的int[行+1][cols+1]; System.out.println(“输入元素”); 对于(i=0;i,java,arrays,matrix,multidimensional-array,Java,Arrays,Matrix,Multidimensional Array,上面的代码运行良好,但我的问题是:使用此逻辑,如果行数和列数不相同,我如何找到矩阵的grandtotal?并告诉我当前代码是否存在任何缺陷。您的代码中没有缺陷。现在,行数和列数不同 声明 public static void main(String[] args) { int rows, cols, j, i; Scanner sc = new Scanner(System.in); System.out.println("enter no of rows and col
grandtotal
?并告诉我当前代码是否存在任何缺陷。您的代码中没有缺陷。现在,行数和列数不同
声明
public static void main(String[] args) {
int rows, cols, j, i;
Scanner sc = new Scanner(System.in);
System.out.println("enter no of rows and columns respectively");
rows = sc.nextInt();
cols = sc.nextInt();
int[][] matrix = new int[rows+1][cols+1];
System.out.println("enter elements");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println("after sum");
int grandtotal = 0;
for(i = 0; i < rows; i++)
{
int rowsum = 0;
int colsum = 0;
for(j = 0; j < cols; j++)
{
rowsum += matrix[i][j];
colsum += matrix[j][i];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
matrix[j][i] = colsum;
}
j = cols;
matrix[i][j] = grandtotal;
matrix[j][i] = colsum;
matrix[rows][j] += matrix[i][j];
now将给出错误的结果,因为现在的行数和列数不同
这可以很容易地通过更改语句来解决
public static void main(String[] args) {
int rows, cols, j, i;
Scanner sc = new Scanner(System.in);
System.out.println("enter no of rows and columns respectively");
rows = sc.nextInt();
cols = sc.nextInt();
int[][] matrix = new int[rows+1][cols+1];
System.out.println("enter elements");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println("after sum");
int grandtotal = 0;
for(i = 0; i < rows; i++)
{
int rowsum = 0;
int colsum = 0;
for(j = 0; j < cols; j++)
{
rowsum += matrix[i][j];
colsum += matrix[j][i];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
matrix[j][i] = colsum;
}
j = cols;
matrix[i][j] = grandtotal;
matrix[j][i] = colsum;
matrix[rows][j] += matrix[i][j];
致:
现在你不需要这份声明了
public static void main(String[] args) {
int rows, cols, j, i;
Scanner sc = new Scanner(System.in);
System.out.println("enter no of rows and columns respectively");
rows = sc.nextInt();
cols = sc.nextInt();
int[][] matrix = new int[rows+1][cols+1];
System.out.println("enter elements");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println("after sum");
int grandtotal = 0;
for(i = 0; i < rows; i++)
{
int rowsum = 0;
int colsum = 0;
for(j = 0; j < cols; j++)
{
rowsum += matrix[i][j];
colsum += matrix[j][i];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
matrix[j][i] = colsum;
}
j = cols;
matrix[i][j] = grandtotal;
matrix[j][i] = colsum;
matrix[rows][j] += matrix[i][j];
因此,通过这些简单的修改,不同列数和行数的代码如下:
matrix[j][i] = colsum;
(i=0;i
{
int rowsum=0;
对于(j=0;j
保持代码的其余部分不变。迭代所有单元格并求和的问题在哪里?如果每行/每列的列/行数不同,则使用matrix[i].length
作为内部循环。或者查找最后一行中的最后一个单元格有问题吗?如果有问题,请尝试row[]=matrix[matrix.length-1];row[row.length-1]=grandtotal;
@Pratik Thacker作为一名计算机工程师,您应该始终考虑重用现有解决方案的解决方案。就像这种情况一样。@Pratik Thacker不仅代码变小了,而且还可以处理与行数和列数有关的任何输入。一些空格和代码格式可以使您的代码可读。