Sabtu, 21 Desember 2013

Array multi dimensi pada JAVA




import javax.swing.JOptionPane;
public class arraymulti{
public static void main(String[]args){
int baris = 0;
int kolom = 0;

baris = Integer.parseInt(JOptionPane.showInputDialog("baris: "));
kolom = Integer.parseInt(JOptionPane.showInputDialog("kolom: "));

int[][] matrixA = new int[baris][kolom];
int[][] matrixB = new int[baris][kolom];
int[][] matrixC = new int[baris][kolom];

for(int x=0; x<baris;x++){
for(int y=0; y<kolom;y++){
matrixA[x][y] = (int)(Math.random()*100);
matrixB[x][y] = (int)(Math.random()*100);
matrixC[x][y] = matrixA[x][y] + matrixB[x][y];
}}

System.out.println("matrixA");
System.out.println("-------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixA[x][y] +"\t");
}
System.out.println();
}
System.out.println();
System.out.println("matrixB");
System.out.println("-------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixB[x][y] +"\t");
}System.out.println(" ");}

System.out.println();
System.out.println("matrix A + B");
System.out.println("------------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixC[x][y] +"\t");
} System.out.println();
}}}

Tidak ada komentar:

Posting Komentar