Pages

Powered by Blogger.

Friday, January 16, 2015

Kode program untuk mengurutkan data berdasarkan nama dan bedasarkan nim dengan java

Kode Program
public static void main(String[] args) {
       Scanner input = new Scanner (System.in);
       int banyak,a;
       String tempnama,tempnim;
       System.out.print("Masukkan banyak data yang akan diinput: ");
       banyak = input.nextInt();
       String nama[]= new String[banyak];
       String nim[] = new String [banyak];
       for(;;){
            System.out.println("======Main menu=====");
            System.out.println("Pilihlah salah satu menu dibawah");
            System.out.println("1. Masukkan data mahasiswa ");
            System.out.println("2. Tampilkan data urut berdasarkan nama");
            System.out.println("3. Tampilkan data urut berdasarkan NIM");
            System.out.println("4. Exit");
            System.out.print("Pilihan anda: ");
            a = input.nextInt();
       switch(a){
           case 1: System.out.println("Masukkan data mahasiswa (nama dan NIM) ");
                   for (int x = 0;x<banyak;x++){
                       System.out.print("NIM Mahasiswa " +(x+1)+" : ");
                       nim[x]=input.next();
                       System.out.print("Nama Mahasiswa "+(x+1)+" : ");
                       nama[x]=input.next();
                       }
           break;
           case 2: for ( int d = 0; d<banyak;d++){
                    for( int b = 1; b<banyak; b++){
                        if ( nama [b-1].compareTo(nama[b])>0){
                            tempnama = nama[b-1];
                            tempnim = nim[b-1];
                            nama [b-1]= nama [b];
                            nim [b-1]= nim [b];
                            nama[b] = tempnama;
                            nim[b] = tempnim;
                         }
                     }
                }
           for (int c = 0;c<banyak;c++){
            System.out.println(nim[c]+" " +nama[c]);
       }break;
           case 3: for ( int d = 0; d<banyak;d++){
                    for( int b = 1; b<banyak; b++){
                        if ( nim [b-1].compareTo(nim[b])>0){
                            tempnama = nama[b-1];
                            tempnim = nim[b-1];
                            nama [b-1]= nama [b];
                            nim [b-1]= nim [b];
                            nama[b] = tempnama;
                            nim[b] = tempnim;
                         }
                     }
                }
           for (int c = 0;c<banyak;c++){
            System.out.println(nim[c]+" " +nama[c]);
       }break;
           case 4: System.exit(0);
           default: System.out.println("Pilihan tidak ada dimenu");
        }
    }
    }

}

1 comment:

 

Blogger news

Blogroll

About