java 输入给定大小矩阵并输出

import java.util.Scanner;
public class Matrix{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the row number of matrix");
        int row = in.nextInt();
        System.out.println("Please enter the col number of matrix");
        int col = in.nextInt();
        System.out.println("Please enter the elements of matrix one by one, using enter to feed the matrix");
        while(in.hasNext()){
            int[][] matrix = new int[row][col];
            for(int i = 0; i < row*col;i++){
                matrix[i/col][i%col] = in.nextInt();
            }
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    System.out.print(matrix[i][j]+" ");
                }
                System.out.println();
            }
        }
        in.close();
    }
}

 

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bddc53d2b717720b51ac833
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞