joption pane

Rabu, 08 Juni 2011

Contoh-contoh program menggunakan JoptionPane

Pertama
import javax.swing.*;
public class tugas_bintang1 {

public static void main(String[] args){
int x;
x=Integer.valueOf(JOptionPane.showInputDialog("Masukan banyak bintang :"));

for(int i=1;i<=x;i++){
for(int j=1;j<=x;j++){
if(j<=i)
System.out.print("*");
else
System.out.print(" ");

}
System.out.println();
}
}
}

Output keluarnya…
run:
*
**
***
****

Sintaks menggunakan switch (pilih)


import java.util.Scanner;
public class SwitchPilihan {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        garis();
        System.out.println("        MENU PILIHAN           ");
        garis();
        System.out.println("1. Menghitung nilai rata-rata  ");
        System.out.println("");
        System.out.println("2. Menghitung bilangan kelipatan 4  ");
        System.out.println("");
        System.out.println("3. Keluar");
        garis();
        System.out.print("Anda pilih nomer :");
       
        int pilih=input.nextInt();

        switch (pilih){
            case 1: {
                System.out.println("Menghitung nilai rata-rata");
                System.out.println("");
                System.out.print("Masukan jumlah data =");
                int n=input.nextInt(); int[] data=new int[10];
                for(int i=1;i<=n;i++){
                    System.out.print("Data ke"+i +"=");
                    data[i]=input.nextInt();
                }
                int jum=0;
                for(int i=1;i<=n;i++){
                    jum=jum +data[i];
                }
                System.out.println("Jumlah   ="+jum);
                int rata=jum/n;
                System.out.println("Rata-rata ="+rata);
                break;
            }

            case 2: {
                System.out.println("Menghitung kelipatan 4");
                System.out.println("");
                System.out.print("Dari   =");
                int nilai_awal=input.nextInt();
                System.out.print("Sampai =");
                int nilai_akhir=input.nextInt();
                    for(int i=nilai_awal;i<=nilai_akhir;i++){
                        if(i%4==0){
                          
                            System.out.println(i);
                        }
                    }
                break;
            }

            default:System.exit(0);
        }
    }
    static void garis(){
        System.out.println("------------------------------------");
    }
    }


Output yang keluar akan seperti dibawah ini !!
run:
------------------------------------
        MENU PILIHAN          
------------------------------------
1. Menghitung nilai rata-rata 

2. Menghitung bilangan kelipatan 4 

3. Keluar
------------------------------------
Anda pilih nomer :2
Menghitung kelipatan 4

Dari   =10
Sampai =20
12
16
20

ulangan

public class Pemrogaman {
    public static void main(String[] args){
        String kal1="STMIK BANI SHALEH";
        String kal2="Pemrogaman 2";

        int i,j;
        for(i=1;i<=3;i++){
            System.out.println("");
            System.out.println(kal1);

            for(j=1;j<=3;j++){
                System.out.println(kal2);
            }
        }
    }

}


Outputnya seperti dibawah ini
run:

STMIK BANI SHALEH
Pemrogaman 2
Pemrogaman 2
Pemrogaman 2

STMIK BANI SHALEH
Pemrogaman 2
Pemrogaman 2
Pemrogaman 2

STMIK BANI SHALEH
Pemrogaman 2
Pemrogaman 2
Pemrogaman 2

htng

public class Hitung {
    public static void main(String[] args){
        int[] data1={0,1,2,3,4,5};
        int[] data2={0,1,2,3,4,5};
        int i,j,jum;

        System.out.println("+ 0 1 2 3 4 5");
        System.out.println("-----------------");
            for (i=0;i<=5;i++){
                System.out.print(i);

            for(j=0;j<=5;j++){
                jum=data1[i]+data2[j];
                System.out.print(" "+jum);
            }
            System.out.println("");

    }

    }
}


Outputnya adalah
run:
+ 0 1 2 3 4 5
-----------------
0 0 1 2 3 4 5
1 1 2 3 4 5 6
2 2 3 4 5 6 7
3 3 4 5 6 7 8
4 4 5 6 7 8 9
5 5 6 7 8 9 10

menhitung nilai maha siswa

menghitung nilai mhs

import java.util.Scanner;
public class Baru {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        garis();
        System.out.println("            MENU PILIHAN                  ");
        garis();
        System.out.println("1. Menhitung Nilai Mahasiswa");
        System.out.println("");
        System.out.println("2. Exit");
        garis();
        System.out.println("");
        System.out.print("Anda pilih no   :");
        int pilih=input.nextInt();

        switch(pilih){
            case 1:{
               
                int[] npm=new int[20];
                String[] nama=new String[20];
                String[] jur=new String[20];
                String[] mk=new String[20];
                int[] n_tgas=new int[20];
                int[] n_uts=new int[20];
                int[] n_uas=new int[20];


                garis();
                System.out.println("    MENGHITUNG NILAI MAHASISWA");
                garis();
                System.out.println("");
                System.out.print("Masukan jumlah data ="); int n=input.nextInt();

                for(int i=1;i<=n;i++){
                    System.out.println("Data ke"+i +":");
                    System.out.print("NPM           :"); npm[i]=input.nextInt();
                    System.out.print("Nama          :"); nama[i]=input.next();
                    System.out.print("Jurusan       :"); jur[i]=input.next();
                    System.out.print("Mata Kuliah   :"); mk[i]=input.next();
                    System.out.print("Nilai tugas   :"); n_tgas[i]=input.nextInt();
                    System.out.print("Nilai UTS     :"); n_uts[i]=input.nextInt();
                    System.out.print("Nilai UAS     :"); n_uas[i]=input.nextInt();
                    double rata=(n_tgas[i]+n_uts[i]+n_uas[i])/3;
                    System.out.println("Nilai rata-rata ="+rata);

                }
                break;
                }
        default:System.exit(0);
        }
        }
static void garis(){
        System.out.println("==========================================");
    }
}



Outputnya
run:
==========================================
            MENU PILIHAN                 
==========================================
1. Menhitung Nilai Mahasiswa

2. Exit
==========================================

Anda pilih no   :1
==========================================
    MENGHITUNG NILAI MAHASISWA
==========================================

Masukan jumlah data =2
Data ke1:
NPM           :432
Nama          :aku
Jurusan       :ti
Mata Kuliah   :pm2
Nilai tugas   :80
Nilai UTS     :70
Nilai UAS     :87
Nilai rata-rata =79.0
Data ke2:
NPM           :434
Nama          :ina
Jurusan       :ti
Mata Kuliah   :pm1
Nilai tugas   :70
Nilai UTS     :80
Nilai UAS     :70
Nilai rata-rata =73.0