membuat kalkulator dengan java
source kode ada di bawah silahkan di copy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calculator.buferetreader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class buferedreader {
public static void main(String args[]) {
double hasil;
String pilih = " ";
boolean t = true;
int x;
double y, z;
String nilai1 = " ";
String nilai2 = " ";
BufferedReader baca = new BufferedReader(new InputStreamReader(System.in));
System.out.println("======== Kalkulator ========n");
System.out.println("============================n");
System.out.println("1. Penjumlahan ");
System.out.println("2. Pembagian ");
System.out.println("3. Pengurangan");
System.out.println("4. Perpangkatan");
System.out.println("5. Perkalian");
System.out.println("6. Keluar");
System.out.println("===========================n");
System.out.print("Pilih angka 1 - 5 untuk menghitung =");
try {
pilih = baca.readLine();
} catch (Exception e) {
}
System.out.print("Masukan angka pertama =");
try {
nilai1 = baca.readLine();
} catch (Exception e) {
}
System.out.print("Masukan angka kedua =");
try {
nilai2 = baca.readLine();
} catch (Exception e) {
}
x = Integer.parseInt(pilih);
y = Double.parseDouble(nilai1);
z = Double.parseDouble(nilai2);
switch (x) {
case 1:
hasil = y + z;
System.out.println("Hasil penjumlahan= " + hasil);
break;
case 2:
hasil = y / z;
System.out.println("Hasil pembagian = " + hasil);
break;
case 3:
hasil = y - z;
System.out.println("Hasil pengurangan = " + hasil);
break;
case 4:
hasil = Math.pow(y, z);
System.out.println("Hasil perpangkatan = " + hasil);
break;
case 5:
hasil = y * z;
System.out.println("Hasil perkalian = " + hasil);
break;
default:
System.out.println("Piliha anda salah!!!");
System.out.println("Maaf yang anda masukan bukan angka!");
System.out.println("Kesalahan aritmetika!");
System.out.println("Kesalahan IO");
}
}
}
0 comments