Last active
October 12, 2021 17:07
-
-
Save defrindr/8ee715b841886c678022ee7a920269de to your computer and use it in GitHub Desktop.
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
import java.util.Scanner; | |
public class Kalkulator { | |
public static void main(String args[]){ | |
double result = 0; | |
Scanner scan = new Scanner(System.in); | |
int bil1; | |
char op, conf; | |
System.out.print("Masukkan bilangan ke-1: "); | |
bil1 = scan.nextInt(); | |
System.out.print("Masukkan operator (+, -, *, /): "); | |
op = scan.next().charAt(0); | |
System.out.print("Masukkan bilangan ke-2: "); | |
int bil2 = scan.nextInt(); | |
result = Hitung(op, bil1, bil2); | |
System.out.println(">> " + bil1 + " " + op + " " + bil2 + " = " + result); | |
System.out.print("Lanjut (y/n) ? "); | |
conf = scan.next().charAt(0); | |
int count = 2; | |
while(conf == 'y') { | |
int bil_n; | |
char op2; | |
double tmp = result; | |
System.out.println("Hasil terakhir: " + result); | |
System.out.print("\nMasukkan operator (+, -, *, /): "); | |
op2 = scan.next().charAt(0); | |
count++; | |
System.out.print("Masukkan bilangan ke-" + count + ": "); | |
bil_n = scan.nextInt(); | |
result = Hitung(op2, result, bil_n); | |
System.out.println(">> " + tmp + " " + op2 + " " + bil_n + " = " + result); | |
System.out.print("Lanjut (y/n) ? "); | |
conf = scan.next().charAt(0); | |
} | |
System.out.print("\nHasilnya: " + result); | |
} | |
public static double Hitung(char op, double bil1, double bil2) { | |
double result = 0; | |
switch(op) { | |
case '+': | |
result = bil1 + bil2; | |
break; | |
case '-': | |
result = bil1 - bil2; | |
break; | |
case '*': | |
result = bil1 * bil2; | |
break; | |
case '/': | |
result = bil1 / bil2; | |
break; | |
default: | |
break; | |
} | |
return result; | |
} | |
} |
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
import java.util.Scanner; | |
class Kalkulator { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int nilai[] = new int[5]; | |
char[] operator = new char[5]; | |
int i = 0; | |
char op; | |
try{ | |
while(i < 5){ | |
nilai[i] = scan.nextInt(); | |
operator[i] = (char) System.in.read(); | |
op = operator[i]; | |
if(op == '=') { | |
break; | |
} | |
i++; | |
} | |
} catch (Exception e) { | |
System.out.println("Error: " + e.toString()); | |
} | |
int result = nilai[0]; | |
for (int start = 1; start <= i; start++) { | |
if(operator[start-1] == '+') { | |
result = result + nilai[start]; | |
}else if(operator[start-1] == '-') { | |
result -= nilai[start]; | |
}else if(operator[start-1] == '/') { | |
result /= nilai[start]; | |
}else if(operator[start-1] == '*') { | |
result *= nilai[start]; | |
} | |
} | |
System.out.println("Hasilnya adalah : "+ result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment