如何从字符串操作转换[sign] b而不用if。

时间:2014-02-02 11:23:20

标签: java string integer character-arrays

代码计算表达式a [sign] b的结果。 可能的操作:+ - * 。我可以在不使用条件的情况下解决此练习吗?

  String result =  "3 + 6 = ";
   String result = outputStream.toString();
        String[] resultArray = result.split(" ");
        Integer first = Integer.parseInt(resultArray[0]);
        Integer second = Integer.parseInt(resultArray[2]);
        String opp = resultArray[1];

        if("+".equals(opp)){
            System.out.println(String.format("%d %s %d = %d", first, opp, second, first + second));
        }
        else if("-".equals(opp)){
            System.out.println(String.format("%d %s %d = %d", first, opp, second, first - second));
        }
        else if("*".equals(opp)){
            System.out.println(String.format("%d %s %d = %d", first, opp, second, first * second));
        }

3 个答案:

答案 0 :(得分:2)

另一种方法是使用开关盒:

    switch (opp.charAt(0)) {
    case '+':
        System.out.println(String.format("%d %s %d = %d", first, opp,
                second, first + second));
        break;
    case '-':
        System.out.println(String.format("%d %s %d = %d", first, opp,
                second, first - second));
        break;
    case '*':
        System.out.println(String.format("%d %s %d = %d", first, opp,
                second, first * second));
        break;
    }

答案 1 :(得分:2)

是的,您可以通过两种方式避免这种情况 - 使用switch并使用Map

使用switch声明:

if (opp.length() != 1)
    // Show error, and exit.
switch(opp.charAt(0)) {
case '+':
    System.out.println(String.format("%d %s %d = %d", first, opp, second, first + second));
    break;
case '-':
    System.out.println(String.format("%d %s %d = %d", first, opp, second, first - second));
    break;
case '*':
    System.out.println(String.format("%d %s %d = %d", first, opp, second, first * second));
    break;
default:
    // Show error...
    break;
}

使用Map<K,V>和专用界面:

interface Operation {
    int calc(int a, int b);
}
Map<String,Operation> ops = new HashMap<String,Operation> {
    {"+", new Op() {public calc(int a, int b) {return a+b;}}}
,   {"-", new Op() {public calc(int a, int b) {return a-b;}}}
,   {"*", new Op() {public calc(int a, int b) {return a*b;}}}
};
...
Operation op = ops.get(opp);
if (op == null)
    // Report an error and exit
System.out.println(String.format("%d %s %d = %d", first, opp, second, op(first, second)));

答案 2 :(得分:1)

基于其他一些答案(使用switch语句或map),我认为最干净的解决方案是将运算符移动到枚举中:

public enum Operator {
    ADDITION("+") {
        public int operate(int a, int b) {
            return a + b;
        }
    },
    SUBTRACTION("-") {
        public int operate(int a, int b) {
            return a - b;
        }
    },
    MULTIPLICATION("*") {
        public int operate(int a, int b) {
            return a * b;
        }
    };

    private String symbol;

    private Operator(String symbol) {
        this.symbol = symbol;
    }

    public abstract int operate(int a, int b);

    public static Operator fromSymbol(String symbol) {
        for (Operator o : values()) {
            if (o.symbol.equals(symbol)) {
                return operator;
            }
        }
        throw new IllegalArgumentException("No operator with symbol " + symbol);
    }
}

然后你有一个非常简单的API,用于从符号中获取正确的运算符(反之亦然),并且很容易添加更多(例如除法)。它还有一个额外的好处,允许您传递Operator的实例,并直接通过名称解决它们(例如Operator.ADDITION)。

String symbol = "*";
Operator o = Operator.fromSymbol(symbol);
int a = 2;
int b = 3;
int result = o.operate(a, b);