以2D阵列打印中间的行和列

时间:2019-03-19 22:47:01

标签: java arrays println

我创建了一个2D数组,用户可以在其中选择其大小和值。但是我在尝试打印其中间的行和列时遇到了麻烦。我该如何解决这个问题?

public static void main(String args[])
{
    int row, col, i, j;
    int a[][]=new int[10][10];

    do{
    Scanner sc=new Scanner (System.in);
    System.out.println("Enter the order of a square matrix :");
    row=sc.nextInt();
    col=sc.nextInt();
    /* checking order of matrix and then if true then enter the values
     * into the matrix a[][]
    */

    if(row == col)
    {
        System.out.println("Enter elements in the matrix:"+row*col);
        for(i=0; i<row; i++)
        {
            for(j=0; j<col; j++)
            a[i][j]=sc.nextInt();
        }
    }
    } while(row != col);
   // Display the entered value
    System.out.println ("You have entered the following matrix");


    for (i=0; i<row; i++)
   {
       for (j=0; j<col; j++)
       System.out.print (a[i][j] + " ");
       System.out.println ();
    }

1 个答案:

答案 0 :(得分:1)

function digitize(n) {
  const number = [];
  const str = n.toString();
  for (const c of str) {
   number.push(+c); 
  }
  return number;
}

console.log(...digitize(1234));