1. 程式人生 > >java 輸入給定大小矩陣並輸出

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