import java.io.*;
import javax.swing.*;
class MenuMatriks
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
int pilih, baris, kolom, i, j, k, barisB, kolomB;
char kembali = ' ';
do
{
System.out.println ("====================================================");
System.out.println (" Menu Matriks ");
System.out.println ("====================================================");
System.out.println ("1. Menjumlahkan matriks");
System.out.println ("2. Mengurangkan matriks");
System.out.println ("3. Perkalian matriks");
System.out.println ("4. Exit");
System.out.println ("====================================================");
System.out.print ("pilihan : ");
pilih = Integer.parseInt(in.readLine());
System.out.println (" ");
switch (pilih)
{
case 1:
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks A : ");
int [][] A = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+A[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] B = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+B[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Hasil penjumlahan : ");
int [][] C = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
C[i][j] = A[i][j] + B[i][j];
System.out.print(" "+C[i][j]+" ");
}
System.out.println("|");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 2:
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks A : ");
int [][] D = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
D[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+D[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] E = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
E[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+E[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Hasil pengurangan : ");
int [][] F = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
F[i][j] = D[i][j] - E[i][j];
System.out.print(" "+F[i][j]+" ");
}
System.out.println(" |");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 3:
do
{
System.out.println("Matriks A : ");
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks B : ");
System.out.print("Ketik jumlah baris : ");
barisB = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolomB = Integer.parseInt(in.readLine());
System.out.println (" ");
if (kolom!= barisB)
{
JOptionPane.showMessageDialog(null,"Matrik A tidak dapat dikalikan dengan Matrik B \n silakan ulangi");
}
}
while(kolom != barisB);
System.out.println("Matriks A : ");
int [][] G = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
G[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+G[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] H = new int[barisB][kolomB];
for (i = 0; i < barisB; i++)
{
System.out.print("|");
for (j = 0; j < kolomB; j++)
{
H[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+H[i][j]+" ");
}
System.out.println("|");
}
System.out.println("Hasil Perkalian : ");
int HasilPerkalianMatrix [][]= new int [baris][kolomB];
for (i=0; i < baris; i++)
{
System.out.print("|");
for (j=0; j < kolomB; j++)
{
int hasil = 0;
for (k = 0; k < baris; k++)
{
hasil+= (G[i][k] * H[k][j]);
HasilPerkalianMatrix[i][j] = hasil;
}
System.out.print(" " + HasilPerkalianMatrix[i][j] + " ");
}
System.out.println("|");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 4:
System.out.println("TERIMA KASIH ");
break;
default:
JOptionPane.showMessageDialog(null,"pilihan tidak ada di menu \n silahkan ulangi");
break;
}
}
while(pilih != 4);
{
System.exit(0);
}
}
}
READ MORE - Membuat Menu Matriks Dengan JAVA
import javax.swing.*;
class MenuMatriks
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
int pilih, baris, kolom, i, j, k, barisB, kolomB;
char kembali = ' ';
do
{
System.out.println ("====================================================");
System.out.println (" Menu Matriks ");
System.out.println ("====================================================");
System.out.println ("1. Menjumlahkan matriks");
System.out.println ("2. Mengurangkan matriks");
System.out.println ("3. Perkalian matriks");
System.out.println ("4. Exit");
System.out.println ("====================================================");
System.out.print ("pilihan : ");
pilih = Integer.parseInt(in.readLine());
System.out.println (" ");
switch (pilih)
{
case 1:
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks A : ");
int [][] A = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+A[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] B = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+B[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Hasil penjumlahan : ");
int [][] C = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
C[i][j] = A[i][j] + B[i][j];
System.out.print(" "+C[i][j]+" ");
}
System.out.println("|");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 2:
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks A : ");
int [][] D = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
D[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+D[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] E = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
E[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+E[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Hasil pengurangan : ");
int [][] F = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
F[i][j] = D[i][j] - E[i][j];
System.out.print(" "+F[i][j]+" ");
}
System.out.println(" |");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 3:
do
{
System.out.println("Matriks A : ");
System.out.print("Ketik jumlah baris : ");
baris = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolom = Integer.parseInt(in.readLine());
System.out.println (" ");
System.out.println("Matriks B : ");
System.out.print("Ketik jumlah baris : ");
barisB = Integer.parseInt(in.readLine());
System.out.print("Ketik jumlah kolom : ");
kolomB = Integer.parseInt(in.readLine());
System.out.println (" ");
if (kolom!= barisB)
{
JOptionPane.showMessageDialog(null,"Matrik A tidak dapat dikalikan dengan Matrik B \n silakan ulangi");
}
}
while(kolom != barisB);
System.out.println("Matriks A : ");
int [][] G = new int[baris][kolom];
for (i = 0; i < baris; i++)
{
System.out.print("|");
for (j = 0; j < kolom; j++)
{
G[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+G[i][j]+" ");
}
System.out.println("|");
}
System.out.println (" ");
System.out.println("Matriks B : ");
int [][] H = new int[barisB][kolomB];
for (i = 0; i < barisB; i++)
{
System.out.print("|");
for (j = 0; j < kolomB; j++)
{
H[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+" : "));
System.out.print(" "+H[i][j]+" ");
}
System.out.println("|");
}
System.out.println("Hasil Perkalian : ");
int HasilPerkalianMatrix [][]= new int [baris][kolomB];
for (i=0; i < baris; i++)
{
System.out.print("|");
for (j=0; j < kolomB; j++)
{
int hasil = 0;
for (k = 0; k < baris; k++)
{
hasil+= (G[i][k] * H[k][j]);
HasilPerkalianMatrix[i][j] = hasil;
}
System.out.print(" " + HasilPerkalianMatrix[i][j] + " ");
}
System.out.println("|");
}
System.out.print("ingin kembali ke menu (Y/N) : ");
kembali = (in.readLine()).charAt(0);
if (kembali == 'N' || kembali == 'n')
{
System.exit(0);
}
break;
case 4:
System.out.println("TERIMA KASIH ");
break;
default:
JOptionPane.showMessageDialog(null,"pilihan tidak ada di menu \n silahkan ulangi");
break;
}
}
while(pilih != 4);
{
System.exit(0);
}
}
}