[英]Java Input scanner to array multidimensional
我想显示我输入的输入数组。 并自动打印。 我想显示我输入的输入值数组。 并会自动打印。
我的代码是这样的:
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
for(int j = 0; j < column; j++) {
System.out.println("Row ["+i+"]: Column "+j+" :");
matrix[i][j] = sc.nextInt(); //Storing input value here
System.out.println(matrix[i][j]);//Output the input value
}
代码如下
for (int i = 0; i < baris; i++)
{
for(int j = 0; j < column; j++) {
// print array data to screen
System.out.println("Data Array "+(i+1) +matrix[i][j]);
}
System.out.println();
}
希望该代码对您有所帮助,请仔细阅读。
打印行和列号,然后输入matix数据并以矩阵形式打印
扫描仪扫描=新的Scanner(System.in);
System.out.println("Enter The Number Of Matrix Rows"); int row = scan.nextInt(); System.out.println("Enter The Number Of Matrix Columns"); int col = scan.nextInt(); //defining 2D array to hold matrix data int[][] matrix = new int[row][col]; // Enter Matrix Data enterMatrixData(scan, matrix, row, col); // Print Matrix Data printMatrixData(matrix, row, col);
} public static void enterMatrixData(Scanner scan,int [] [] matrix,int row,int col){
System.out.println("Enter Matrix Data"); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i][j] = scan.nextInt(); } }
}
公共静态无效printMatrixData(int [] []矩阵,int行,int col){
System.out.println(“您的矩阵为:”);
for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { System.out.print(matrix[i][j]+"\\t"); } System.out.println(); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.