2 Dimensional Array (Array 2 Dimensi)
Assalamualaikum.
On this occasion here I create an Application with 2-dimensional Array Display task, maybe this is simple and quite a bit difficult also for me who is still the stage of learning and beginners.
Conditions ;
On this occasion here I create an Application with 2-dimensional Array Display task, maybe this is simple and quite a bit difficult also for me who is still the stage of learning and beginners.
Conditions ;
- Number of rows 4
- Number of colom 4
- The user inputs a value (digits 2 digits) on each combination of rows and columns
- System displays the value inputted
- If there is a value entered there is a number 4, then the value of replace with the character '**'
For the steps I created, here I use the integer data type (int) that is converted to the String data type. And here I use looping for because it is used to retrieve values on an array.
For how to convert my data here using method valueOf ()
- Declaration of class of data type to be changed
- Use the Method
- Declare variables of data types that want to change
String[][] nilai1
nilai1[row][colom]= String.valueOf(nilai[row][colom]);
For More Clear can be checked Source code below;
package aray2;
import java.util.Scanner ;
public class Aray2 {
public static void main(String[] args) {
int [][] nilai = new int [4][4] ;
String[][] nilai1 = new String[4][4];
int row , colom ;
System.out.println("");
for(row=0;row<nilai.length;row++){
for(colom=0;colom<nilai[row].length;colom++){
Scanner bacaRecord = new Scanner(System.in);
System.out.print("Nilai Tabel baris["+row+"] kolom["+colom+"]= ");
nilai[row][colom] = bacaRecord.nextInt();
}
}
System.out.println("\n-----------Tampilan nilai -----------------");
for(row=0;row<nilai.length;row++){
for(colom=0;colom<nilai[row].length;colom++){
System.out.print(" | "+nilai[row][colom]);
}
System.out.print(" |\n");
}
System.out.println("------------------------------------------");
System.out.println("\n------Tampilan Nilai Berubah--------");
for(row=0;row<nilai.length;row++){
for(colom=0;colom<nilai[row].length;colom++){
nilai1[row][colom]= String.valueOf(nilai[row][colom]);
if (nilai1[row][colom].contains("4")){
nilai1[row][colom]= "**";
System.out.print(" | "+nilai1[row][colom]);
}
else {
System.out.print(" | "+nilai[row][colom]);
}
}
System.out.print(" |\n");
}
System.out.println("---------------------------------");
}
}
If Less Clear can see the picture below ;
For Sample Output Results Viewing Tasks ;
Nilai Tabel baris[0] kolom[0]= 14
Nilai Tabel baris[0] kolom[1]= 18
Nilai Tabel baris[0] kolom[2]= 94
Nilai Tabel baris[0] kolom[3]= 17
Nilai Tabel baris[1] kolom[0]= 66
Nilai Tabel baris[1] kolom[1]= 97
Nilai Tabel baris[1] kolom[2]= 16
Nilai Tabel baris[1] kolom[3]= 44
Nilai Tabel baris[2] kolom[0]= 12
Nilai Tabel baris[2] kolom[1]= 32
Nilai Tabel baris[2] kolom[2]= 78
Nilai Tabel baris[2] kolom[3]= 46
Nilai Tabel baris[3] kolom[0]= 99
Nilai Tabel baris[3] kolom[1]= 96
Nilai Tabel baris[3] kolom[2]= 23
Nilai Tabel baris[3] kolom[3]= 42
-----------Tampilan nilai -----------------
| 14 | 18 | 94 | 17 |
| 66 | 97 | 16 | 44 |
| 12 | 32 | 78 | 46 |
| 99 | 96 | 23 | 42 |
------------------------------------------
------Tampilan Nilai Berubah--------
| ** | 18 | ** | 17 |
| 66 | 97 | 16 | ** |
| 12 | 32 | 78 | ** |
| 99 | 96 | 23 | ** |
---------------------------------
Result picture output :
Okay, Thankyou so much .. 😉
Komentar
Posting Komentar