Java输入扫描器到多维数组

时间:2013-10-08 09:05:40

标签: java multidimensional-array

我想显示我输入的输入数组。并自动打印。我想显示我输入的输入值数组。并将自动打印。

我的代码是这样的:

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

任何人都可以帮助我。

4 个答案:

答案 0 :(得分:4)

   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

答案 1 :(得分:0)

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
}

答案 2 :(得分:0)

代码如下

        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();
}

我希望此代码对您有所帮助,请查看。

答案 3 :(得分:0)

  • 打印行和列号,然后输入matix数据和 以矩阵形式打印

    扫描仪扫描=新扫描仪(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();
              }
          }
    

    }

    public static void printMatrixData(int [] [] matrix,int row,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();
        }       
    

    }