1

このコードは、式 a [sign] b から結果を計算します。 可能な操作: + - * . 条件 if を使わずにこの演習を解くことはできますか?

  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

はい、次の 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)));
于 2014-02-02T11:32:09.053 に答える
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 に答える
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 に答える