我试图创建一个Android应用程序,这将是一个简单的数学游戏。它有多个难度级别,包括简单,中等,难度,非常难。简易级别涉及2个整数(6 + 6)之间的算术问题,3之间的中等(2 + 2-1),4(2 + 2-1 / 3)之间的硬和5(2 + 3 * 5)之间的非常硬-3 + 2)。我必须生成随机整数和随机算术运算。我能够显示每个难度级别的问题和操作,但我找不到正确的答案。无论我输入什么答案,程序都会显示错误。例如,如果我回答3 + 3-1为5或任何其他,它将始终显示错误。对于2个整数我使用了switch语句,并且它工作正常,但涉及多个算术运算的问题总是错误的任何想法如何做到这一点。我将代码放在下面
private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3;
private String[] operators = {"+", "-", "x", "/"};
operator = random.nextInt(operators.length);
switch(operator)
{
case ADD_OPERATOR:
answer = operand1+operand2;
break;
case SUBTRACT_OPERATOR:
answer = operand1-operand2;
break;
case MULTIPLY_OPERATOR:
answer = operand1*operand2;
break;
case DIVIDE_OPERATOR:
answer = operand1/operand2;
break;
default:
break;
}
question.setText(operand1+" "+operators[operator]+" "+operand2);
//Operand 1 and Operand 2 are 2 Random Integers
public void onClick(View view)
{
//button clicked
if(view.getId()==R.id.enter){
//enter button
String answerContent = answerTxt.getText().toString();
if(!answerContent.endsWith("?"))
{
//we have an answer}
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
int exScore = getScore();
if(enteredAnswer==answer){
//correct
scoreTxt.setText("Score: "+(exScore+1));
response.setImageResource(R.drawable.tick);
response.setVisibility(View.VISIBLE);
}else{
//incorrect
scoreTxt.setText("Score: 0");
response.setImageResource(R.drawable.cross);
response.setVisibility(View.VISIBLE);
}
chooseQuestion();
}}
else if(view.getId()==R.id.clear){
//clear button
answerTxt.setText("= ?");
}
else {
//number button
response.setVisibility(View.INVISIBLE);
int enteredNum = Integer.parseInt(view.getTag().toString());
if(answerTxt.getText().toString().endsWith("?"))
answerTxt.setText("= "+enteredNum);
else
answerTxt.append(""+enteredNum);
}
}
我将用户答案存储为答案文字。 有关如何为多个算术运算执行此操作的任何帮助。感谢