Lập trình Java - Nhập xuất ma trận ngẫu nhiên trong Java
Nhập xuất ma trận ngẫu nhiên trong Java
import java.util.Random;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ADMIN
*/
public class Bai28 {
/**
* @param args the command line arguments
*/
public static void NhapMaTran(int b[][], int d, int c){
Random rand = new Random();
for(int i=0; i<d; i++)
for(int j=0; j<c; j++)
b[i][j]=rand.nextInt(30);
}
//Xuất Ma Trận
public static void xuatMaTran(int b[][], int d, int c){
for(int i=0; i<d; i++){
System.out.println();
for(int j=0; j<c; j++)
System.out.print("\t"+b[i][j]+"\t");
}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.println("\n\nNhap so dong: ");
int d= input.nextInt(); //dòng
System.out.println("\n\nNhap so cot: ");
int c = input.nextInt(); //cột
int b[][] = new int[d][c];
NhapMaTran(b,d,c);
System.out.print("Noi dung ma tran: ");
xuatMaTran(b,d,c);
}
}
Kết quả chạy chương trình:
Nhap so dong:
5
Nhap so cot:
5
Noi dung ma tran:
7 10 25 19 3
12 25 28 17 17
29 10 5 7 9
22 1 13 20 25
22 9 23 21 5