Pages

Powered by Blogger.

Friday, January 16, 2015

Program kalkulator java

Source code program

public class Main {
 static int jumlah, inputan;
    static Scanner input = new Scanner (System.in);
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       String menu;
        char operator = '=';
        System.out.println("-- Program Kalkuator Java --");
        System.out.print("Masukkan angka yang akan dioperasikan : ");
        inputan = input.nextInt();
        jumlah = inputan;
        while (true){
System.out.println("==============================");
        System.out.println    ("     SIMBOL KALKULATOR     ");
          System.out.println("==============================");
          System.out.println("    (+)    ");
          System.out.println("    (-)    ");
          System.out.println("    (/)    ");
          System.out.println("    (x)    ");
          System.out.println("    (=)    ");
          System.out.println("==============================");
          System.out.println("     x (Keluar) ");
          System.out.println("==============================");
          menu=input.next();
            //mengubah String menjadi char
            if (menu.equals("+")) operator = '+';
            else if (menu.equals("-"))  operator = '-';
            else if (menu.equals("*")) operator = '*';
            else if (menu.equals("/")) operator = '/';
            else if (menu.equals("=")) operator = '=';
            else if (menu.equals("x")) operator = 'x';
            else operator = '!';

            switch (operator){
                case '+':
                    System.out.print(+jumlah+ " + ");
                    penjumlahan(inputan);
                    break;
                case '-':
                    System.out.print(+jumlah+ " - ");
                    pengurangan(inputan);
                    break;
                case '*':
                    System.out.print(+jumlah+ " * ");
                    perkalian (inputan);
                    break;
                case '/':
                    System.out.print(+jumlah+ " / ");
                    pembagian (inputan);
                    break;
                case '=':
                    System.out.println("Hasil = "+ jumlah);
                    break;
                case 'x':
                    System.out.println("Keluar");
                    System.out.println();
                    System.exit(0);
                    break;
                default :
                    System.out.println("Input salah.");
                    break;
            }
        }
    }
    public static void penjumlahan(int inputan) {
        inputan = input.nextInt();
        jumlah +=inputan;
    }
    public static void pengurangan(int inputan) {
        inputan = input.nextInt();
        jumlah -=inputan;
    }
    public static void perkalian(int inputan) {
        inputan = input.nextInt();
        jumlah *=inputan;
    }
    public static void pembagian(int inputan) {
        inputan = input.nextInt();
        jumlah /=inputan;
    }

}

Screenshoot Program

No comments:

Post a Comment

 

Blogger news

Blogroll

About