Java输入扫描器到多维数组 [英] Java Input scanner to array multidimensional
本文介绍了Java输入扫描器到多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示我输入的输入数组.并自动打印.我想显示我输入的输入值数组.并会自动打印.
I want to display an array of input that I input. and in print automatically. I want to display the array of input values that I input. and will be printed automatically.
我的代码是这样的:
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
Input total row : 2 Input total column : 2
第 [0] 行:第 0 列:1
Row [0]: Column 0 : 1
第 [0] 行:第 1 列:2
Row [0]: Column 1 : 2
第 [1] 行:第 0 列:10
Row [1]: Column 0 : 10
第 [1] 行:第 1 列:11
Row [1]: Column 1 : 11
数据数组 1 : 1,2数据数组 2 : 10,11
Data Array 1 : 1,2 Data Array 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输入扫描器到多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文