0

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

我的代码是这样的:

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

4 回答 4

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
于 2013-10-08T09:13:15.390 回答
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();
}

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

于 2013-10-08T09:13:14.927 回答
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
}
于 2013-10-08T09:10:14.533 回答
0
  • 打印行号和列号,然后输入矩阵数据并以矩阵形式打印

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

    } 公共静态无效 enterMatrixData(扫描仪扫描,int[][] 矩阵,int 行,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();
        }       
    

    }

于 2018-04-17T05:38:19.380 回答