如何定期更新整数

时间:2016-02-08 18:12:57

标签: android android-layout android-fragments android-intent

所以我有一个String [],其中包含数学表达式,如" 3 + 6 ="或" 5 + 9/8 ="。 我有一个函数获取数组,循环遍历数组并循环,它分割数组,以便我有一个String []包含数学运算,如+, - ,*,/和另一个包含数字的int []问题。

然后该函数将答案放入一个名为answers的int []中。我的问题是,为什么当我输出答案[0]时,我只能看到第一个数字,而不是第一个问题的答案?

编辑=我发现问题出在哪里。它发生在我的评估方法中。我不知道为什么:

public int evaluateAnswerTwoOperations(String operation, int numberOne, int numberTwo) {

    switch(operation){

        case "+":
            Log.d("usingsubtraction","plus2nmbers");
            return numberOne + numberTwo;
        case "-":
            Log.d("usingsubtraction","usingsubtraction");
            return  numberOne - numberTwo;
        case "/":
            Log.d("usingsubtraction","usingdivision");
            return numberOne / numberTwo;
        default:
            Log.d("usingsubtraction","usingmultiplication");
            return numberOne*numberTwo;

    }

1 个答案:

答案 0 :(得分:0)

这样的事情:

import android.util.Log;

public class TwoNumberOperation {

    private final String PLUS = "+";
    private final String MINUS = "-";
    private final String MULTIPLY = "*";
    private final String DIVIDE = "/";
    private final String EQUAL = "=";

    private String operation;
    private int numberOne;
    private int numberTwo;

    public TwoNumberOperation(String operation, int numberOne, int numberTwo) {
        if (operation.equalsIgnoreCase(PLUS) ||
                operation.equalsIgnoreCase(MINUS) ||
                operation.equalsIgnoreCase(MULTIPLY) ||
                operation.equalsIgnoreCase(DIVIDE)) {

            this.operation = operation;
            this.numberOne = numberOne;
            this.numberTwo = numberTwo;
        }
        else {
            Log.d("error", "error: invalid operation");
            this.operation = "";
            this.numberOne = 0;
            this.numberTwo = 0;
        }
    }

    public int getAnswer() {

        switch (operation) {

            case PLUS:
                Log.d("addition", "addition");
                return numberOne + numberTwo;
            case MINUS:
                Log.d("subtraction", "subtraction");
                return numberOne - numberTwo;
            case MULTIPLY:
                Log.d("multiplication", "multiplication");
                return numberOne / numberTwo;
            case DIVIDE:
                Log.d("division", "division");
                return numberOne / numberTwo; // note the returning an int
            default:
                Log.d("error", "error");
                return -999999999;
        }
    }

    @Override
    public String toString() {
        return numberOne + operation + numberTwo + EQUAL;
    }

}