1

代码从 Expression 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));
        }
4

3 回答 3

2

另一种方法是使用 switch-case:

    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;
    }
于 2014-02-02T11:29:09.823 回答
2

是的,您可以通过两种方式避免这种情况 - 使用 aswitch和使用 a 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)));
于 2014-02-02T11:32:09.053 回答
1

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

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);
于 2014-02-02T11:44:34.903 回答