java程序:任意输入一个4*4矩阵,实现输出并计算其中元素之和。
4个回答
展开全部
import javax.swing.JOptionPane;
public class sumMatrix {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Enter a 4 by 4 matrix row by row:");
int[][] matrix = new int[4][4];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix.");
JOptionPane.showMessageDialog(null, "Sum of the Matrix is " + sumMatrix(matrix));
// double sum = sumMatrix(matrix);
}
public static double sumMatrix(int[][] m) {
int sum = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
sum = sum + m[i][j];
return sum;
}
}
public class sumMatrix {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Enter a 4 by 4 matrix row by row:");
int[][] matrix = new int[4][4];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix.");
JOptionPane.showMessageDialog(null, "Sum of the Matrix is " + sumMatrix(matrix));
// double sum = sumMatrix(matrix);
}
public static double sumMatrix(int[][] m) {
int sum = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
sum = sum + m[i][j];
return sum;
}
}
追答
亲,记得采纳哦
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一个矩阵就是一个二维数组,用二维数组接收数据,用双层for循环累加,搞定
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
早忘了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-05-19
引用pieryon的回答:
import javax.swing.JOptionPane;
public class sumMatrix {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Enter a 4 by 4 matrix row by row:");
int[][] matrix = new int[4][4];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix.");
JOptionPane.showMessageDialog(null, "Sum of the Matrix is " + sumMatrix(matrix));
// double sum = sumMatrix(matrix);
}
public static double sumMatrix(int[][] m) {
int sum = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
sum = sum + m[i][j];
return sum;
}
}
import javax.swing.JOptionPane;
public class sumMatrix {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Enter a 4 by 4 matrix row by row:");
int[][] matrix = new int[4][4];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix.");
JOptionPane.showMessageDialog(null, "Sum of the Matrix is " + sumMatrix(matrix));
// double sum = sumMatrix(matrix);
}
public static double sumMatrix(int[][] m) {
int sum = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
sum = sum + m[i][j];
return sum;
}
}
展开全部
matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix."));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询