Tuesday, November 13, 2012

Ulangan Harian Java (2) - Soal 1

Kali ini saya akan share ulangan harian java kedua mengenai array dan pemrograman OOP, disini saya akan membahas soal yang pertama terlebih dahulu, yaitu :

Buatlah program berikut dengan menggunakan Array 2 Dimensi :
Dengan inputan:

jumlah baris : ..

jumlah kolom : ..

Outputnya :

Jumlah baris           : 2
Jumlah kolom        : 4
Data anda                 : 1 2 3 4
5 6 7 8

Berikut source codenya :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Ulangan;

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
*
* @author Mirra
*/
public class soal1 {

public static void main(String[] args) {
// TODO code application logic here
int baris, kolom;
int b = 1 ;
Scanner input = new Scanner(System.in);
System.out.print("Jumlah Baris = ");
baris = input.nextInt();
System.out.print("Jumlah kolom = ");
kolom = input.nextInt();
int[][]tabel = new int[baris][kolom];
for (int i = 0; i < baris; i++) {
for (int j = 0; j < kolom; j++) {
tabel[i][j] = b ;
b++;
}
}
System.out.println("Data Anda : ");
for (int i = 0; i < baris; i++) {
for (int j = 0; j < kolom; j++) {
System.out.print(tabel[i][j]+" ");
}
System.out.println();
System.out.print(" ");

}
System.out.println();
}

}


Hasil compilenya adalah :
run:
Jumlah Baris = 2
Jumlah kolom = 4
Data Anda :
1 2 3 4
5 6 7 8

BUILD SUCCESSFUL (total time: 5 seconds)

No comments:

Post a Comment