如何使用多个符号进行拆分并根据符号调用方法

时间:2016-10-17 03:07:46

标签: java string

我正在制作一个计算器来测试我能做些什么,而且我遇到了一个小问题。我试图在我的"输入"上输入一个输入参数。只需让它们输入数字作为字符串并解析符号两侧的双精度数。但是,我不知道该怎么做。我正在考虑使用if语句,比如if(symbol)run方法。代码如下:

public class Calculation_Controls extends JPanel implements ActionListener, KeyListener{

public double A, B;

public String input(String nums){
    String[] split = nums.split("[-.+,/,*,^]");
    int left = Integer.parseInt(split[0]);
    int right = Integer.parseInt(split[1]);

}

//What I am trying to invoke for the "if (symbol)"
public double add(double a, double b){
    this.a = A;
    this.b = B;
    return (a + b);
}

public double subtract(double a, double b){
    this.a = A;
    this.b = B;
    return a - b;
}

public double multiply(double a, double b){
    this.a = A;
    this.b = B;
    return a * b;
}

public double divide(double a, double b){
    this.a = A;
    this.b = B;
    return a / b;
}

public double pwr(double a, double b){
    this.A = a;
    this.B = b;
    return (Math.pow(a, b));
}

2 个答案:

答案 0 :(得分:0)

假设您期望的输入格式是" 5 * 6"的一般字符串。 :

public class Calculation_Controls {

    private String[] operators = new String[] {"-","+","/","*","^"};

    public void input(String nums){

        //add: check input is valid 

        String operator = findSymbol(nums);
        if(operator == null ) {
            System.out.println("Invalid input");
            return;
        }
        String[] split = nums.split(operator);
        int left = Integer.parseInt(split[0]);
        int right = Integer.parseInt(split[1]);
        double result = 0;

        switch (operator) {

        case "-":
            result = subtract(left, right);
            break;
        case "+":
            result = add(left, right);
            break;
        case "/":
            result = divide(left, right);
        case "*":
            result = multiply(left, right);
            break;
        case "^":
            result = pwr(left, right);  
            break;
        default: System.out.println("Invalid operator");    
        }

    }

    private String findSymbol(String nums) {

        for( String operator : operators) {
            if(nums.contains(operator))
                return operator; 
        }

        return null;
    }

    public double add(double a, double b){
        return (a + b);
    }

    public double subtract(double a, double b){

        return a - b;
    }

    public double multiply(double a, double b){

        return a * b;
    }

    public double divide(double a, double b){

        return a / b;
    }

    public double pwr(double a, double b){

        return (Math.pow(a, b));
    }
}

注意:我没有测试它。

答案 1 :(得分:0)

你可以使用switch case OR if,

或者使用如下界面。

 interface Operation {
        public double resultFor(double left, double right);
    }

class Calculation_Controls extends JPanel implements ActionListener, KeyListener{
    public double a, b;
    Map<Character, Operation> operations = new HashMap<Character, Operation>();

    public Calculation_Controls() {
        operations.put('*', new multiply());
        operations.put('-', new subtract());
        operations.put('+', new Add());
        operations.put('/', new divide());
        operations.put('^', new pwr());
    }

    public String input(String nums) {
        String[] split = nums.split("[-.+,/,*,^]");
        for (int i = 0; i < split.length; i++) {
            System.out.println("" + split[i].length());
        }
        char operation = nums.charAt(split[0].length());
        int left = Integer.parseInt(split[0]);
        int right = Integer.parseInt(split[1]);
        double result = 0;
        Operation op = operations.get(Character.valueOf(operation));
        if (op != null) {
            result = op.resultFor(left, right);
            System.out.println("Result :" + result);
        } else {
            System.out.println("Error: Unknown operator");
        }
        return String.valueOf(result);
    }

    class Add implements Operation {
        @Override
        public double resultFor(double left, double right) {
            return (left + right);
        }

    }

    class subtract implements Operation {
        @Override
        public double resultFor(double left, double right) {
            return left - right;
        }

    }

    class divide implements Operation {
        @Override
        public double resultFor(double left, double right) {
            return left / right;
        }

    }

    class multiply implements Operation {
        @Override
        public double resultFor(double left, double right) {
            return left * right;
        }

    }

    class pwr implements Operation {
        @Override
        public double resultFor(double left, double right) {
            return (Math.pow(left, right));
        }

    }
}

使用它:

Calculation_Controls inst = new Calculation_Controls(); inst.input( “2 ^ 4”);