gpt4 book ai didi

java - 单击时正确生成整数

转载 作者:行者123 更新时间:2023-12-02 03:45:57 25 4
gpt4 key购买 nike

关于调用函数时生成的整数,我的代码遇到了问题 - 我的代码是一个数学游戏,用户用 1 到 12 的数字回答随机算术问题,我可以这样做,但代码似乎在允许用户回答上一个问题之前生成新的随机值,这意味着用户回答屏幕上未显示的下一个问题。我认为它在我对特定行进行评论的代码中描述得更好。

public class MainActivity extends AppCompatActivity {




Random r = new Random();
int points1 = 0;


@Override
protected 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);
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));
}

最后,我想要实现的是在创建(onCreate)时生成用户可以回答的值,然后在用户输入和提交后,应用程序应该生成新值并允许用户再次回答。我并不期望为我完成全部工作,而是希望能够引导我找到答案,因为我真的很想学习和理解我的问题。

我尝试了不同的方法,但我想这只是我的经验和知识阻碍了我找到解决方案 - 我是一个初学者并且自学成才。

编辑随机运算符(operator)问题;

公共(public)类 MainActivity 扩展 AppCompatActivity {

Random r = new Random();


int points1 = 0;
int value1, value2;
TextView Number1;
TextView Number2;
TextView Answer;
TextView displayPoints;
EditText Attempt;
TextView Operator;

@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);
Operator = findViewById(R.id.operator);
displayPoints = findViewById(R.id.points);
Attempt = findViewById(R.id.Attempt);

value1 = r.nextInt(12 - 1) + 1;
value2 = r.nextInt(12 - 1) + 1;
Number1.setText("" + value1);
Number2.setText("" + value2);

int operator = r.nextInt(4 - 1) + 1; //HERE
switch (operator)
{
case 1: operator = '+';
break;
case 2: operator = '-';
break;
case 3: operator = '*';
break;
case 4: operator = '/';
break;
}
Operator.setText("" + operator);
}




public void onSubmitCheck(View view) {
reset();

}

public void reset() {


int operator = r.nextInt(4 - 1) + 1; //here
switch (operator)
{
case 1: operator = '+';
break;
case 2: operator = '-';
break;
case 3: operator = '*';
break;
case 4: operator = '/';
break;
}

int userAnswer = Integer.parseInt(Attempt.getText().toString());
if (userAnswer == value1 + operator + value2) {
Answer.setText("Correct!");
point();
displayPoints.setText("" + points1);

} else {
Answer.setText("The answer was: " + (value1 + value2));
}

value1 = r.nextInt(12 - 1) + 1;
value2 = r.nextInt(12 - 1) + 1;
Number1.setText("" + value1);
Number2.setText("" + value2);
Operator.setText("" + Operator);

}
public void point() {
points1 = points1 + 1;

}


public void onSubmitNext(View view) {
reset();
}

}

最佳答案

将 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);

关于java - 单击时正确生成整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56808445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com