Contoh Java Simple Calculator

import java.io.*;

public class calc {

    public static void main(String[] args) {

		System.out.println("cara menggunakan");
		System.out.println("masukan angka 1 ");
		System.out.println("masukan angka 2 ");
    	System.out.println("tombol pilih cuma terdiri dari 1 sampai 4");
		System.out.println("1 untuk kali");
		System.out.println("2 untuk bagi");
		System.out.println("3 untuk tambah");
		System.out.println("4 untuk kurang");

    	BufferedReader m = new BufferedReader(new InputStreamReader(System.in));
		String input1 = " ";
		String input2 = " ";
		int x1,x2;
		int kali,bagi,tambah,kurang;
		int pilih;
		System.out.println("Masukkan Angka 1 : ");
		try{
			input1 = m.readLine();

			//	System.out.println(input1);
			}catch(IOException e){
			System.out.println(e);
		}
		System.out.println("Masukkan angka 2: ");
		try{
			input2 = m.readLine();
			}catch(IOException e){
			System.out.println(e);
			}
		x1 = Integer.parseInt(input1);
		x2 = Integer.parseInt(input2);

		kali = x1*x2;
		bagi = x1/x2;
		tambah = x1+x2;
		kurang = x1-x2;

		String pilih2 = " ";
		System.out.println("Masukkan angka pilihan : " + "\t");
		try{
			pilih2 = m.readLine();
			pilih = Integer.parseInt(pilih2);
			switch(pilih){
				case 1 : System.out.println("hasilnya " + kali);break;
				case 2 : System.out.println("hasilnya " + bagi);break;
				case 3 : System.out.println("hasilnya " + tambah);break;
				case 4 : System.out.println("hasilnya " + kurang);break;
				default: System.out.println("data error");
			}
			}catch(IOException e){
			System.out.println(e);
		}
    }

}

silahkan comment jika suka…:D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s