Percabangan switch dengan java

package com.bhe.cabang;

import java.util.Scanner;

/**
 *
 * @author bahrie
 */
public class CabangSwitch {
     public static void main(String[] args) {
        int status=0;//harus diinisialisasi, coba 0 nya kamu hapus, maka akan ada pesan error.
         System.out.println("1. gaji 50000");
         System.out.println("2. gaji 40000");
         System.out.println("3. gaji 30000");
        Scanner scan;
        System.out.print("Masukkan pilihan [nomor]: ");
        try {
            scan=new Scanner(System.in);
            status=Integer.parseInt(scan.nextLine());
        }catch (Exception e){
            System.out.println("error "+e.toString());
        }

        int gaji=0;
        switch(status){
            case 1:
                gaji=50000;
            break;//jangan lupa pake break, kalo ga pake maka program akan ngecek kebawah terus.
            case 2:
                gaji=40000;
                break;
            case 3 :
                gaji=30000;
                break;
            default :
                System.out.println("Masukkan Nomor salah");
        }
        System.out.println("gaji= "+gaji);
    }

}

silahkan dipahami dulu.
download sourcecodenya disini.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: