Monday, 27 August 2018

Contoh script java menentukan ordo kolom, ordo baris, dan matrik


import java.io.*;

class satu
{
                public static void main (String[]args) throws IOException
                {
                DataInputStream x = new DataInputStream (System.in);
                {             
                                int matriks [][]=new int [3][3];
                                int matriksA [][]= new int[3][3];
                                int matriksB [][]=new int[3][3];
                                int matriksC [][]=new int[3][3];
                                System.out.print("Batas ordo kolom = ");
                                int kolom=Integer.parseInt(x.readLine());
                                System.out.print("Batas ordo baris = ");
                                int baris=Integer.parseInt(x.readLine());
               
                                System.out.println("Matriks A ");
                               
                                for (int y=1; y<=kolom; y++)
                                {
                                               
                                                for (int z=1; z<=baris; z++)
                                                {
                                                                System.out.print("A [" +y+ " , " +z+ "] =");
                                                                matriksA[y][z]=Integer.parseInt(x.readLine()); 
                                                }
                                }
                                               
//

System.out.println("Matriks B ");
                               
                                for (int y=1; y<=kolom; y++)
                                {
                                               
                                                for (int z=1; z<=baris; z++)
                                                {
                                                                System.out.print("B [" +y+ " , " +z+ "] =");
                                                                matriksB[y][z]=Integer.parseInt(x.readLine());
                                                               
                                                }
                                                }
//
System.out.println("Matriks C ");
                               
                                for (int y=1; y<=kolom; y++)
                                {
                                               
                                                for (int z=1; z<=baris; z++)
                                                {
                                                                System.out.print("A [" +y+ " , " +z+ "] =");
                                                                matriksC[y][z]=matriksA[y][z]+matriksB[y][z];
                                                               
                                                }
                                                }
//

                               
                                for (int y=1; y<=kolom; y++)
                                {
                                               
                                                for (int z=1; z<=baris; z++)
                                                {
                                                                System.out.print(matriksC[y][z]+"");
                                               
                                                               
                                                }
                                                }                                                                                             
                                                               
                                                }
                                                }
                               
                }
               

Hasilnya :

--------------------Configuration: <Default>--------------------
Batas ordo kolom = 2
Batas ordo baris = 3
Matriks A
A [1 , 1] =4
A [1 , 2] =5
A [1 , 3] =3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
                at satu.main(matrik.java:32)

Process completed.

Download slengkapnya disini

No comments:

Post a Comment