检查用户是否正确

时间:2016-10-21 09:40:58

标签: java android android-studio if-statement double

我目前正在开展一个学校项目(一个小型​​安卓游戏),到目前为止,我编写的代码在活动random equation启动后2秒生成InGame并显示在textview中。再过5秒钟,生成第二个等式并以不同的文本视图显示。现在,用户必须通过按下按钮biggersmaller来确定第二个等式的结果是否大于第一个等式。如果它是正确的,那么应该显示下一个等式,它将继续这样,直到用户决定错误为止。 到目前为止,这是我的代码: (第一个等式的代码):

// Generate random equation and display it in textview

String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
    TextEquation.setText(equation);

    // Evaluate the result of the equation

    double doubleAnswer1 = eval(equation);

    String stringAnswer = Double.toString(doubleAnswer1);

    TextView textAnswer = (TextView)findViewById(R.id.textView4);
    textAnswer.setText(stringAnswer);
}

(第二个等式的代码(基本上与第一个等式相同,但字符串和双精度的名称不同)):

String equation2;
static double doubleAnswer2;
public void start2() {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation2 = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation2 += numbers.get(i);
        equation2 += operations.get(i);
    }
    equation2 += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView3);
    TextEquation.setText(equation2);

    // Evaluate the result of the equation

    double doubleAnswer2 = eval(equation2);

    String stringAnswer = Double.toString(doubleAnswer2);

    TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
    textAnswer.setText(stringAnswer);
}

这是我的onCreate代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ingame);

    // Display first equation 2 seconds after the activity is launched

    final Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {
            start1();
        }
    }, 2000);

    final Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {
            start2();
        }
    }, 7000);

    // Check if user was right or wrong

    final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
    final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else {
                Log.v("TAG", "you are wrong");
            }
        }
    };

    buttonBigger.setOnClickListener(listener);
    buttonSmaller.setOnClickListener(listener);
}

应用程序正确启动并显示first and second等式,但当我按下其中一个按钮时,它会在logcat you are wrong中告诉我,但我认为100%正确。但是,如果我调试应用程序,它会告诉我doubleAnswer1和doubleAnswer2是both = 0.这就是为什么它会告诉我'你错了'。我不知道如何解决这个问题,也许我需要将doubleAnswer1和doubleAnswer2存储在某个地方。 我真的不知道该怎么做,所以如果有人知道该做什么,这对我有帮助。

如果我的问题中有任何不清楚的地方,请随时提出,我会尽力澄清问题。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为你的问题在于:

double doubleAnswer1 = eval(equation);

我做了一个快速的互联网搜索,但没有找到任何名为eval()的本机函数。相反,你应该查看java脚本引擎管理器:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));

或exp4j,如下所示: Executing math equation in Android

编辑: 改变以下内容:

double doubleAnswer1 = eval(equation);

为:

doubleAnswer1 = eval(equation);

同样对doubleAnswer2

也一样