我想显示我输入的输入数组。并自动打印。我想显示我输入的输入值数组。并会自动打印。
我的代码是这样的:
public class ReadArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input total row : ");
int row = sc.nextInt();
System.out.print("Input total column : ");
int column = sc.nextInt();
int [][] matrix = new int[row][column];
for (int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++) {
System.out.println("Row ["+i+"]: Column "+j+" :");
matrix[i][j] = sc.nextInt();
}
}
}
}
我想要这样的结果:
输入总行:2 输入总计列:2
行 [0]:第 0 列: 1
行 [0]:第 1 列: 2
行 [1]:第 0 列: 10
行 [1]:第 1 列: 11
数据数组 1 : 1,2 数据数组2:10,11
任何人都可以帮助我。
最佳答案
String result="";//this variable for the last line which print the result
for (int i = 0; i < row; i++) {
result=result+"Data Array "+i+" :";
for (int j = 0; j < column; j++) {
System.out.println("Row [" + i + "]: Column " + j + " :");
matrix[i][j] = sc.nextInt();
result=result+matrix[i][j]+", ";
}
}
System.out.println(result);////for the final result
关于Java 输入扫描仪到多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19243429/