分数不应该增加到它应该的时候

问题描述 投票:0回答:2

我想要做的很简单。我想生成随机数(rand1和rand2)并让用户给出总和的正确答案。有2个按钮,任何一个都可以找到正确的答案。我正在使用randDecider变量来确定是否应该在第一个或第二个按钮上显示正确的答案。 randDecider是1或2。

我遇到的问题有时候当我点击正确的答案时,分数不会增加。事实证明,有时当我按错了答案时,分数会增加。因此,我认为无论答案是否正确,分数都会增加50/50。

    protected void setRandom(View v) {

//Assigning random values to ints
        rand1 = r.nextInt(5) + 1;
        rand2 = r.nextInt(5) + 1;
        randDecider = r.nextInt(2)+1 ;

//The sum of the randoms
        sum = rand1 + rand2;

//Setting the texts of the random values
        randTV1.setText(rand1 + "");
        randTV2.setText(rand2 + "");

/*If the random deciding number is 1, set the correct answer
on the choice1 button*/
        if (randDecider == 1){
            choice1.setText(sum+"");
            choice2.setText(sum+1+"");

//If the correct answer was chosen, increment the score and set the text
            if(v.getId()==R.id.choice1){
                score++;
                scoreTV.setText(score+"");
            }
        }
/*If the random deciding number is 2, set the correct answer
to the choice2 button*/
        if (randDecider == 2){
            choice1.setText(sum+1+"");
            choice2.setText(sum+"");

//If the correct answer was chosen, increment the score and set the text
            if(v.getId()==R.id.choice2){
                score++;
                scoreTV.setText(score+"");
            }
        }
android
2个回答
1
投票

我建议拆分两个不同的函数a)创建和呈现要解决的问题,b)检查响应然后c)引入一个类变量来存储答案,直到出现另一个问题(这将是在响应b之后) )。

所以你可以

  • a)在课堂上添加一行 int correctanswer;
  • b)添加一种设置问题的方法,例如setProblem(最初调用然后在c内调用))
  • c)添加检查响应的方法,例如setResponse在调整分数时调用setProblem方法。单击任一按钮时调用setResponse方法。
  • d)添加一个调用以初始调用setProblem。
  • e)设置2个onclick监听器以调用setResponse方法。

以下可能是一个决议(根据您的问题): -

public class MainActivity extends AppCompatActivity {

    int score, correctanswer;
    TextView scoreTV, randTV1, randTV2;
    Button choice1, choice2;
    Random r = new Random(System.currentTimeMillis());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // get UI components
         scoreTV = (TextView) this.findViewById(R.id.score);
         randTV1 = (TextView) this.findViewById(R.id.rand1);
         randTV2 = (TextView) this.findViewById(R.id.rand2);
         choice1 = (Button) this.findViewById(R.id.choice1);
         choice2 = (Button) this.findViewById(R.id.choice2);

         // Initialise
         score = 0;
         scoreTV.setText(Integer.toString(score));
         setProblem();

         // Button Listeners
         choice1.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 checkResponse((Button)v);
             }
         });
         choice2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 checkResponse((Button) v);
             }
         });
    }

    // Set the problem
    public void setProblem() {

        //Assigning random values to ints
        int rand1 = r.nextInt(5) + 1;
        int rand2 = r.nextInt(5) + 1;
        int randDecider = r.nextInt(2)+1 ;

        //The sum of the randoms
        int sum = rand1 + rand2;
        correctanswer = sum;

        //Setting the texts of the random values
        randTV1.setText(rand1 + "");
        randTV2.setText(rand2 + "");

        /*If the random deciding number is 1, set the correct answer
          on the choice1 button*/
        if (randDecider == 1){
            choice1.setText(sum+"");
            choice2.setText(sum+1+"");
        } else {
            choice2.setText(sum+"");
            choice1.setText(sum+1+"");
        }
    }


    // Check the user's response (called by onClick listeners)
    public void checkResponse(Button v) {    
        if ((new Integer(v.getText().toString()) == correctanswer)) {
            score++;
            scoreTV.setText(score+"");
        }
        setProblem();
    }
}

1
投票

在方法的顶部,您有几行生成随机数和决策程序。这些是正确的。

然后,您显示随机数并将答案放在正确的按钮上,这可能也是正确的。

但是,同时检查是否选择了正确的按钮。这意味着您要检查用户按下的最后一个按钮,而不是他们的答案。

解决此问题的一种方法是保存总和和正确的答案位置至少一次旋转。更改您的setRandom方法以生成数字并将其设置为现在的屏幕,还可以将正确答案保存到外部变量。

然后,在按钮的onPressed方法中,检查按下的按钮是否正确,增加分数,并调用setRandom在屏幕上添加新问题。

您的代码中的问题源于您在屏幕上显示问题时检查答案的事实。编程愉快!

© www.soinside.com 2019 - 2024. All rights reserved.