我在处理调用函数时生成的整数的代码时遇到了麻烦-我的代码本来是一款数学游戏,用户可以用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)供用户回答,然后在用户输入并提交应用程序后,应用程序应生成新值并允许用户再次回答。我并不期望为我完成全部工作,但可以使我真正想学习和理解我的问题的答案带给我答案。
我尝试了不同的方法,但我想这仅仅是我的经验和知识使我无法找到解决方案-我是一个初学者,并且自学成才。
编辑随机运营商问题;
公共类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();
}
}
答案 0 :(得分:0)
将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);
答案 1 :(得分:0)
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,这将是一个更好的选择。它可以帮助您理解代码。