gpt4 book ai didi

java - 无法同步 Android 乘法应用程序上问题的答案

转载 作者:行者123 更新时间:2023-12-01 20:20:01 26 4
gpt4 key购买 nike

我的简单 Android 应用程序有问题。当我启动它时,问题似乎比我作为输入输入的答案领先一步。例如,如果问题是 3x3 = ?,它将只接受下一个问题的正确答案。所以我在当前问题中给出的任何答案都将永远是错误的。我通过连续输入相同的答案来测试这一点。任何指示将不胜感激。我希望这是有道理的!

public class MainActivity extends AppCompatActivity {

int value3;
int answer;
EditText answer_field;

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

final ImageView leftNumber = findViewById(R.id.left_number);

final ImageView rightNumber = findViewById(R.id.right_number);

Button myButton = findViewById(R.id.answer_button);

final int[] numberArray = new int[]{
R.drawable.number_0,
R.drawable.number1,
R.drawable.number2,
R.drawable.number3,
R.drawable.number4,
R.drawable.number5,
R.drawable.number6,
R.drawable.number7,
R.drawable.number8,
R.drawable.number9,
};
leftNumber.setImageResource(numberArray[0]);
rightNumber.setImageResource(numberArray[0]);


myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


// Create a random number generator
Random randomNumberGenerator = new Random();


int value1 = randomNumberGenerator.nextInt(10);




leftNumber.setImageResource(numberArray[value1]);

// Create a new random number
int value2 = randomNumberGenerator.nextInt(10);

// Set the right dice image using an image from the diceArray.
rightNumber.setImageResource(numberArray[value2]);
answer_field = findViewById(R.id.answer_field);
answer = valueOf(answer_field.getText().toString());
value3 = value1 * value2;
if(value3 == answer) {

showToast("Correct");
}

}

});

}


public void showToast(String text){
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
}

}

谢谢!

最佳答案

您在点击答案按钮时生成值,这意味着每次用户点击答案按钮时,要相乘的值都会随机设置(或重置);将生成值的逻辑放在 onclick 之外,因此当用户单击它时只会进行比较,有点像这样:

[...]

leftNumber.setImageResource(numberArray[0]);
rightNumber.setImageResource(numberArray[0]);

// Create a random number generator
Random randomNumberGenerator = new Random();

int value1 = randomNumberGenerator.nextInt(10);

leftNumber.setImageResource(numberArray[value1]);

// Create a new random number
int value2 = randomNumberGenerator.nextInt(10);

// Set the right dice image using an image from the diceArray.
rightNumber.setImageResource(numberArray[value2]);
answer_field = findViewById(R.id.answer_field);

myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
answer = valueOf(answer_field.getText().toString());
value3 = value1 * value2;
if(value3 == answer) {
showToast("Correct");
}
}
});

[...]

关于java - 无法同步 Android 乘法应用程序上问题的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58940575/

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