我有这段代码
=======
int row = Integer.parseInt(JOptionPane.showInputDialog("Enter number of rows");
int col = Integer.parseInt(JOptionPane.showInputDialog("Enter number of columns");
int[][] sum = new int[row][col];
int x,y;
int result;
for(x=0;x<row;x++) {
for (y=0;x<col;y++) {
int inpx = Integer.parseInt(JOptionPane.showInputDialog("Value for " + x + "," + y);
result = result + inpx
我的问题是如何显示我输入的行中每个值的总和请帮助
答案 0 :(得分:0)
我不确定你对你的问题的意思,但如果你想看到行和列的总和,你可以使用这个
int row = Integer.parseInt(JOptionPane.showInputDialog("Enter row"));
int col = Integer.parseInt(JOptionPane.showInputDialog("Enter column"));
int[][] sum = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
JOptionPane.showMessageDialog(frame, "Result: "+ (i + j));
}
}
如果您只想显示消息,请使用showMessageDialog,而不是showInputDialog
如果这是你想要的,你也可以调整循环以使用sum数组。虽然在我的例子中它没用,因为它们没有价值。
修改强>:
int[][] sum = new int[5][5];
for (int i = 0; i < sum.length; i++) {
for (int j = 0; j < sum[i].length; j++) {
sum[i][j] = Integer.parseInt(JOptionPane.showInputDialog(frame, "Enter value for (" + i + "," + j + ")"));
}
}
int row = Integer.parseInt(JOptionPane.showInputDialog("Enter row"));
int col = Integer.parseInt(JOptionPane.showInputDialog("Enter column"));
int value = sum[row][col];
int row2 = Integer.parseInt(JOptionPane.showInputDialog("Enter second row"));
int col2 = Integer.parseInt(JOptionPane.showInputDialog("Enter second column"));
JOptionPane.showMessageDialog(frame, String.format("(%d,%d)[%d] * (%d,%d)[%d] = %d", row, col, sum[row][col], row2, col2, sum[row2][col2], value * sum[row2][col2]));