Created
March 18, 2016 08:35
-
-
Save ardianta/4c603073712590683b60 to your computer and use it in GitHub Desktop.
Program kalkulator sederhana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pertemuan7; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author petanikode | |
*/ | |
public class KalkulatorSederhana { | |
public static void main(String[] args) { | |
boolean keluar = false; | |
float hasil, a, b; | |
while (keluar == false){ | |
// tampilkan menu | |
String pilihan = JOptionPane.showInputDialog("Pilih Menu:\n" | |
+ "1. Tambah\n" | |
+ "2. Kurang\n" | |
+ "3. Kali\n" | |
+ "4. Bagi\n" | |
+ "5. Keluar"); | |
switch(pilihan){ | |
case "1": | |
a = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai a:")); | |
b = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai b:")); | |
hasil = a + b; | |
JOptionPane.showMessageDialog(null, "Hasil " + a +" + "+ b +" = " + hasil); | |
break; | |
case "2": | |
a = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai a:")); | |
b = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai b:")); | |
hasil = a - b; | |
JOptionPane.showMessageDialog(null, "Hasil " + a +" - "+ b +" = " + hasil); | |
break; | |
case "3": | |
a = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai a:")); | |
b = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai b:")); | |
hasil = a * b; | |
JOptionPane.showMessageDialog(null, "Hasil " + a +" * "+ b +" = " + hasil); | |
break; | |
case "4": | |
a = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai a:")); | |
b = Integer.valueOf(JOptionPane.showInputDialog("Inputkan nilai b:")); | |
hasil = a / b; | |
JOptionPane.showMessageDialog(null, "Hasil " + a +" / "+ b +" = " + hasil); | |
break; | |
case "5": | |
String yakin = JOptionPane.showInputDialog("Yakin ingin keluar?\n" | |
+ "1. Ya\n" | |
+ "2. Tidak"); | |
if (yakin.equals("1")) { | |
keluar = true; | |
System.exit(0); | |
} | |
break; | |
default: | |
JOptionPane.showMessageDialog(null, "Pilihan '" + pilihan +"' tidak ada!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment