基本上我要做的是在屏幕上显示一个随机表达式并检查随机表达式的答案是否等于用户输入的答案,如果正确的话,在#是的时候在屏幕上显示“CORRECT”按下一次并在再次按下#时将下一个随机表达式抬起..
我已经对此进行了3天的研究但得到了以下代码:
package org.example.question;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity implements View.OnClickListener {
int fnum0;
/** Called when the activity is first created. */
//variable for different questions
int snum0,fnum1, snum1,fnum2, snum2,fnum3, snum3,
fnum4, snum4,fnum5, snum5,fnum6, snum6,fnum7, snum7,
fnum8, snum8,fnum9, snum9, answer;
//variable and type declaration for buttons and text
Button keyOne;
Button keyTwo;
Button keyThree;
Button keyFour;
Button keyFive;
Button keySix;
Button keySeven;
Button keyEight;
Button keyNine;
Button keyDel;
Button keyZero;
Button keyHash;
Button keySubtract;
TextView display;
TextView display1;
int answer0;
TextView correct;
String str;
int q;
private int click;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display text on screen
display1 = (TextView)findViewById(R.id.Guess);
display = (TextView)findViewById(R.id.Title);
correct= (TextView)findViewById(R.id.CORRECT);
int operation = (int) ((double) ((Math.random() * 10)));
//generate random expression with random operators and display "?" if the answer is one digit or"??" if the answer is greater than
fnum0 = (int)((double) (Math.random() * 10));
snum0 = (int)((double) (Math.random() * 10));
if(operation == 0)
str = fnum0+ "+" + snum0+ "=" + ((fnum0+snum0<10)? "?" : "??");
else if(operation == 1)
str = fnum0 + "-" + snum0+ "=" + ((fnum0-snum0<10)? "?" : "??");
else if(operation == 2)
str = fnum0 + "*" + snum0+ "=" + ((fnum0*snum0<10)? "?" : "??");
else
str = fnum0 + "/" + snum0+ "=" + ((fnum0/snum0<10)? "?" : "??");
display.setText(str);
answer0=fnum0+snum0;
///Code for correct and incorrect
//assigning names to each keypad
keyOne= (Button) findViewById(R.id.keypad_1);
keyTwo= (Button) findViewById(R.id.keypad_2);
keyThree= (Button) findViewById(R.id.keypad_3);
keyFour= (Button) findViewById(R.id.keypad_4);
keyFive = (Button) findViewById(R.id.keypad_5);
keySix= (Button) findViewById(R.id.keypad_6);
keySeven = (Button) findViewById(R.id.keypad_7);
keyEight = (Button) findViewById(R.id.keypad_8);
keyNine = (Button) findViewById(R.id.keypad_9);
keyZero = (Button) findViewById(R.id.keypad_0);
keySubtract = (Button) findViewById(R.id.keypad_subtract);
keyHash = (Button) findViewById(R.id.keypad_hash);
keyDel = (Button) findViewById(R.id.delete);
//setting button to produce an event when each button is pressed
keyOne.setOnClickListener(this); keyTwo.setOnClickListener(this); keyThree.setOnClickListener(this);
keyFour.setOnClickListener(this); keyFive.setOnClickListener(this); keySix.setOnClickListener(this);
keySeven.setOnClickListener(this); keyEight.setOnClickListener(this); keyNine.setOnClickListener(this);
keySubtract.setOnClickListener(this); keyHash.setOnClickListener(this); keyDel.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId()){
//what happens when hash button is pressed
case R.id.keypad_hash:
if(click==1 && "CORRECT".equals(display.getText().toString()))
{
correct.setText("CORRECT");
click=2;
}
if(click == 2)
{
display.setText(str);
}
else if("CORRECT".equals(display.getText().toString()))
{
fnum0 = (int) ((double) ((Math.random() * 10)));
snum0 = (int) ((double) ((Math.random() * 10)));
}
else;
fnum0 = (int) ((double) ((Math.random() * 10)));
snum0 = (int) ((double) ((Math.random() * 10)));
//generates random number between 0 to 9 with random operators for first and sceond number
break;
case R.id.keypad_1:
String str1 = display.getText().toString();
display.setText(str1.replace("?", "1"));
break;
case R.id.keypad_2:
String str2 = display.getText().toString();
display.setText(str2.replace("?", "2"));
break;
case R.id.keypad_3:
String str3 = display.getText().toString();
display.setText(str3.replace("?", "3"));
break;
case R.id.keypad_4:
String str4 = display.getText().toString();
display.setText(str4.replace("?", "4"));
break;
case R.id.keypad_5:
String str5 = display.getText().toString();
display.setText(str5.replace("?", "5"));
break;
case R.id.keypad_6:
String str6 = display.getText().toString();
display.setText(str6.replace("?", "6"));
break;
case R.id.keypad_7:
String str7 = display.getText().toString();
display.setText(str7.replace("?", "7"));
break;
case R.id.keypad_8:
String str8 = display.getText().toString();
display.setText(str8.replace("?", "8"));
break;
case R.id.keypad_9:
String str9 = display.getText().toString();
display.setText(str9.replace("?", "9"));
break;
case R.id.delete:
break;
case R.id.keypad_0:
String str0 = display.getText().toString();
display.setText(str0.replace("?", "0"));
break;
case R.id.keypad_subtract:
display.setText("-");
break;
}
}
public void requestFocus() {
// TODO Auto-generated method stub
}
}
感谢您的帮助
答案 0 :(得分:0)
我认为你过于复杂化了。我会设置一个简单的界面,包括一个可编辑的答案,一个按钮,说你已经输入了你想要的答案,并在按钮的onClick()方法,检查可编辑文本,看看是否这是正确的答案。