Buat Program Array 2 Dimensi
Ketentuan:
- Jumlah baris 4
- Jumlah kolom 4
- user menginput nilai (angka- 2digit) pada setiap kombinasi baris dan kolom
- 1. system menampilkan nilai yang diinput
- 2. Jika ada nilai yang diinput terdapat angka 4, maka nilai diganti dengan karakter ‘ ** ’
Source Code :
/*
* 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.
*/
package array;
import java.util.Scanner;
/**
*
* @author Alam dwi gunawan
*/
public class Array {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[][] array = new String[4][4];
int cek;
String bintang = "";
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length;j++){
System.out.print("Input Angka baris["+i+"]"+" kolom["+j+"]= ");
array[i][j] = scan.next();
}
System.out.println("\n");
}
System.out.println("\n");
System.out.println("Hasil Nilai Inputan");
for (String[] array1 : array) {
for (int j = 0; j<array.length; j++) {
System.out.print("|" + array1[j] + "|");
bintang = array1[j];
cek = bintang.indexOf("4");
array1[j] = cek>=0 ? "**" : array1[j];
}
System.out.println("");
}
System.out.println("------------------------------------");
System.out.println("");
System.out.println("Hasil Nilai Berubah");
for (String[] array1 : array) {
for (int j = 0; j<array.length; j++) {
System.out.print("|" + array1[j] + "|");
}
System.out.println();
}
}
}
Contoh program ketika di jalankan :
Tidak ada komentar:
Posting Komentar