Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Calculator {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- boolean z = true;
- System.out.println("command list : +,*,-,/,%,^");
- String[] operation = new String[]{"+", "*", "-", "/", "%"};
- while (z) {
- System.out.println("Enter example");
- String example = sc.nextLine();
- String[] strArr = new String[operation.length];
- String oper = null;
- for (int i = 0; i < operation.length; i++) {
- if (example.contains(operation[i])) {
- oper = operation[i];
- strArr = example.split("\\" + operation[i]);
- break;
- }
- }
- double intArr[] = new double[strArr.length];
- for (int i = 0; i < strArr.length; i++) {
- intArr[i] = Double.valueOf(strArr[i]);
- }
- double x = intArr[0];
- double y = intArr[1];
- switch (oper) {
- case "+":
- System.out.println(sum(x, y));
- break;
- case "/":
- System.out.println(division(x, y));
- break;
- case "*":
- System.out.println(multi(x, y));
- break;
- case "%":
- System.out.println(percent(x, y));
- break;
- case "-":
- System.out.println(substr(x, y));
- break;
- case "^":
- default:
- }
- }
- }
- public static double sum(double x, double y) {
- return x + y;
- }
- public static double division(double x, double y) {
- return x / y;
- }
- public static double multi(double x, double y) {
- return x * y;
- }
- public static double percent(double x, double y) {
- while (x > 0) {
- x -= y;
- }
- x += y;
- return x;
- }
- public static double substr(double x, double y) {
- return x - y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement