Metnum

[java] Membuat kalkulator sederhana

METNUMBuatlah program kalkulator sederhana yang meminta 2 buah nilai dan sebuah operator, kemudian tampilkan hasilnya ke layar. Operator yang dapat dipakai adalah +, -, *, dan /.

Jawab

//https://irwananwar.wordpress.com
import java.util.Scanner;
public class kalkulator {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.printf("Input Angka pertama : ");
		float A=s.nextFloat();
		System.out.printf("Input Operator : ");
		String O=s.next();
		System.out.printf("Input Angka kedua : ");
		float B=s.nextFloat();
		switch (O) {
		case "*":
			System.out.printf("\n%.2f * %.2f = %.2f",A,B,(A*B));
			break;
		case "/":
			if(B!=0)
			System.out.printf("\n%.2f / %.2f = %.2f",A,B,(A/B));
			else System.out.printf("\nSemua bilangan tidak dapat dibagi 0 ");
			break;
		case "+":
			System.out.printf("\n%.2f + %.2f = %.2f",A,B,(A+B));
			break;
		case "-":
			System.out.printf("\n%.2f - %.2f = %.2f",A,B,(A-B));
			break;
		default:
			System.out.printf("\nTidak ada operator "+O);
		}
	}
}

One thought on “[java] Membuat kalkulator sederhana

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