为了账号安全,请及时绑定邮箱和手机立即绑定

单击时正确生成整数

单击时正确生成整数

慕码人2483693 2023-02-23 14:45:00
关于调用函数时生成的整数的代码,我遇到了问题 - 我的代码是一个数学游戏,用户可以用 1 到 12 的数字回答随机算术问题,我可以做到,但代码似乎正在生成在允许用户回答上一个问题之前生成新的随机值,这意味着用户回答了屏幕上未显示的下一个问题。我认为在我对特定行进行评论的代码中对其进行了更好的描述。public class MainActivity extends AppCompatActivity {Random r = new Random();int points1 = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    int value1 = r.nextInt(12 - 1) + 1;             //1st randomized set of values    int value2 = r.nextInt(12 - 1) + 1;             // ^    TextView Number1 = findViewById(R.id.number1);    Number1.setText(""+value1);                            //assigns randomized values to TextViews    TextView Number2 = findViewById(R.id.number2);    Number2.setText(""+value2);                            // ^^}public void onSubmitCheck (View view) {    reset();                                               //When button Submit is clicked}       //^ ^ Issue starts here when we click "submit" - the code generates new random values before checking the old ones against the mathematical expression in line 50..        //..meaning the user is already answering to the SECOND question before even attempting the first one.public void reset(){    int value1 = r.nextInt(12 - 1) + 1;            //Assigns 2nd randomized values    int value2 = r.nextInt(12 - 1) + 1;            // ^        TextView Number1 = findViewById(R.id.number1);        Number1.setText("" + value1);        TextView Number2 = findViewById(R.id.number2);        Number2.setText("" + value2);    TextView Answer = findViewById(R.id.Answer);    TextView displayPoints = findViewById(R.id.points);    EditText Attempt = findViewById(R.id.Attempt);所以最后,我想要实现的是在用户可以回答的创建 (onCreate) 上生成值,然后在用户输入和提交之后,应用程序应该生成新值并允许用户再次回答。我并不期待为我完成完整的工作,而是可以引导我找到答案的想法,因为我真的很想学习和理解我的问题。我尝试了不同的方法,但我想这只是我的经验和知识阻碍了我找到解决方案——我是一个初学者和自学成才的人。
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

使 value1 和 value2 成为全局值,并仅在将其与旧值进行比较后才重置这些值。也做了一些其他的重新安排。


这可能是您的代码:


public class MainActivity extends AppCompatActivity {



Random r = new Random();

int points1 = 0;

int value1, value2;

TextView Number1;

TextView Number2;

TextView Answer;

TextView displayPoints;

EditText Attempt;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    Number1 = findViewById(R.id.number1);

    Number2 = findViewById(R.id.number2);

    Answer = findViewById(R.id.Answer);

    displayPoints = findViewById(R.id.points);

    Attempt = findViewById(R.id.Attempt);


    value1 = r.nextInt(12 - 1) + 1;             //1st randomized set of values

    value2 = r.nextInt(12 - 1) + 1;             // ^

    Number1.setText("" + value1);                            //assigns randomized values to textEdits

    Number2.setText("" + value2);                            // ^^


}


public void onSubmitCheck(View view) {

    reset();                                               //When button Submit is clicked


}       //^ ^ Issue starts here when we click "submit" - the code generates new random values before checking the old ones against the mathematical expression in line 50..

//..meaning the user is already answering to the SECOND question before even attempting the first one.


public void reset() {


    int userAnswer = Integer.parseInt(Attempt.getText().toString());    //compares inputted value to an answer from line 50

    if (userAnswer == value1 + value2) {                                    //LINE 50

        Answer.setText("Correct!");

        point();

        displayPoints.setText("" + points1);


    } else {

        Answer.setText("Wrong :( Answer was: " + (value1 + value2));

    }


    value1 = r.nextInt(12 - 1) + 1;            //Assigns 2nd randomized values

    value2 = r.nextInt(12 - 1) + 1;            // ^

    Number1.setText("" + value1);

    Number2.setText("" + value2);


查看完整回答
反对 回复 2023-02-23
?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

static int points1 = 0; 


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    TextView Number1 = findViewById(R.id.number1);

    TextView Number2 = findViewById(R.id.number2);

    TextView Answer = findViewById(R.id.Answer);

    EditText Attempt = findViewById(R.id.Attempt);

    TextView displayPoints = findViewById(R.id.points);


    generateNumbers();


}


public void onSubmitCheck (View view) {

    checkResult();

    Attempt.clear();                                              


private void generateNumbers () {

    Random r = new Random();

    value1 = r.nextInt(12 - 1) + 1;

    value2 = r.nextInt(12 - 1) + 1;


    Number1.setText(""+value1);                   

    Number2.setText(""+value2);


}


void checkResult() {

    int userAnswer = Integer.parseInt(Attempt.getText().toString());   

    if (userAnswer == value1 + value2) {                 

        Answer.setText("Correct!");


        points1++;


        displayPoints.setText(""+ points1);


    } else {

        Answer.setText("Wrong :( Answer was: " + (value1 + value2));

    }

}

试试这个,从一开始这段代码就可以工作。如果您使用另一个名为Next 的按钮来生成下一个 Value ,这将是一个更好的选择。它可以帮助您理解代码。


查看完整回答
反对 回复 2023-02-23
  • 2 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信