試圖評估一個簡單的數學問題的正確答案。這是正確的方式去抓取輸入並將其轉換爲一個int以便與另一個int共處嗎?將edittext輸入轉換爲整數
public void checkAnswer(){
int mm;
//answer = editText.getText().toString();
mm = Integer.parseInt(editText.getText().toString());
if (mm == correctAnswer){
score++;
scoredisplay.setText("" + score);
}
這是我如何設置correctAnswer
public void getQuestion(){
random1 = (int)(Math.random()*100);
random2 = (int)(Math.random()*10);
int i1 = Min + (int)(Math.random() * ((Max - Min) + 1));
if(i1 == 1){
question = (random1 + "+" + random2);
correctAnswer = random1 + random2;
editTextEquation.setText(question);
}
if(i1 == 2){
question = (random1 + "-" +random2);
correctAnswer = random1 - random2;
editTextEquation.setText(question);
}
if(i1 == 3){
question = random1 + "/" +random2;
correctAnswer = random1/random2;
editTextEquation.setText(question);
}
if(i1 == 4){
question = (random1 + "*" +random2);
correctAnswer = random1 * random2;
editTextEquation.setText(question);
}
else{
score--;
scoredisplay.setText("" + score);
}
按鈕
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:
getQuestion();
checkAnswer();
break;
嘿,謝謝兄弟,我換了他們,櫃檯正在工作!您是否知道我如何將問題數量限制爲10,然後在最後顯示用戶分數?假定我需要某種循環。 – Abs90 2012-03-06 19:49:43