简单的数学游戏 - Android

时间:2017-06-27 08:02:53

标签: java android math

我是android新手。我试着创造一个数学难题。一旦用户点击开始,随机生成两个数字,并且有四个按钮显示答案。但是只有一个是正确的,其他是不正确的。如果只允许一个操作,如加法或减法,它工作正常。但是,当我允许加法,减法和乘法时,我在按钮中得到重复的答案。有人可以告诉我哪里出错了。下面的代码工作正常,因为只允许添加。

    public void generateQuestion(){

    Random rand = new Random();

    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    sumTextView.setText(Integer.toString(a) + "+" +Integer.toString(b)); //sumTextView shows the operation on the screen
    locationOfCorrectAnswer = rand.nextInt(4);
    answers.clear();
    int incorrectAnswer;
    for(int i=0; i<4;i++){
        if(i==locationOfCorrectAnswer){

            answers.add(a+b);

        }   else {

            incorrectAnswer = rand.nextInt(41);
            while(incorrectAnswer == a+b){

                incorrectAnswer = rand.nextInt(41);

            }
            answers.add(incorrectAnswer);

        }

    }

    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));

}

以下代码无效。

public void generateQuestion(){

    Random rand = new Random();

    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    ArrayList<String> mathOperations = new ArrayList<String>();
    mathOperations.add("+");
    mathOperations.add("-");
    mathOperations.add("X");

    String randomOperator = mathOperations.get(rand.nextInt(3));

    locationOfCorrectAnswer = rand.nextInt(4);
    answers.clear();
    int incorrectAnswer;
   for(int i=0; i<4;i++){
        if(i==locationOfCorrectAnswer){
            if(randomOperator.equals("+")){
                a = rand.nextInt(21);
                b = rand.nextInt(21);
                sumTextView.setText(Integer.toString(a) + randomOperator +Integer.toString(b));
                answers.add(a+b);
            }else if(randomOperator.equals("-")){
                a = rand.nextInt(21);
                b = rand.nextInt(21);
                if(a>b){
                    answers.add(a-b);
                } else{
                    sumTextView.setText(Integer.toString(b) + randomOperator +Integer.toString(a));
                    answers.add(b-a);
                }

            }else if(randomOperator.equals("X")){

                a=rand.nextInt(14);
                b=rand.nextInt(14);
                sumTextView.setText(Integer.toString(a) + randomOperator +Integer.toString(b));
                answers.add(a*b);
            }
        }

        else {

                if(randomOperator.equals("+") || randomOperator.equals("-")) {
                    incorrectAnswer = rand.nextInt(61);
                    while(incorrectAnswer == a+b || incorrectAnswer == a-b ){
                        incorrectAnswer = rand.nextInt(61);
                }
                    answers.add(incorrectAnswer);

                } else{
                    incorrectAnswer = rand.nextInt(200);
                    while(incorrectAnswer == a*b){
                        incorrectAnswer = rand.nextInt(200);
                    }
                    answers.add(incorrectAnswer);
                }
        }
    }

    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));

}

0 个答案:

没有答案