android - 如何在测验应用程序中检查洗牌数组的答案
问题描述
我在本网站上关注了另一篇关于如何随机播放测验应用程序的问题和答案的帖子,但我需要帮助(a)在 textview 中显示问题和(b)检查来自 edittext 的答案,因为我不知道如何从洗牌数组中调用这些。
主要活动文件 -
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button submit_btn, next_btn;
TextView scoremm6, question;
EditText answer;
String manswer;
public TextView getScoreT2() {
return scoremm6;
}
private int mScore = 0;
private boolean done;
private int QuestionNo = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
findViewById(R.id.submit_btn).setEnabled(true);
final Button enter_btn = (Button) findViewById(R.id.submit_btn);
answer = (EditText) findViewById(R.id.answer);
scoremm6 = (TextView) findViewById(R.id.scoremm6);
question = (TextView) findViewById(R.id.question);
scoremm6.setText("Score: " + mScore);
List<Questions> questions = new ArrayList<Questions>();
questions.add(
new Questions(
"3 x 5",
"15"
)
);
questions.add(
new Questions(
"4 x 5",
"20"
)
);
questions.add(
new Questions(
"5 x 5",
"25"
)
);
questions.add(
new Questions(
"6 x 5",
"30"
)
);
questions.add(
new Questions(
"7 x 5",
"35"
)
);
questions.add(
new Questions(
"8 x 5",
"40"
)
);
questions.add(
new Questions(
"9 x 5",
"45"
)
);
questions.add(
new Questions(
"10 x 5",
"50"
)
);
questions.add(
new Questions(
"11 x 5",
"55"
)
);
questions.add(
new Questions(
"12 x 5",
"60"
)
);
questions.add(
new Questions(
"2 x 4",
"8"
)
);
questions.add(
new Questions(
"3 x 4",
"12"
)
);
questions.add(
new Questions(
"4 x 4",
"16"
)
);
questions.add(
new Questions(
"5 x 4",
"15"
)
);
Collections.shuffle(questions);
//这是我需要帮助从洗牌数组中检索正确答案的地方
Button Submit = (Button) findViewById(R.id.submit_btn);
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (done == false) {
String answer = ((EditText) findViewById(R.id.answer)).getText().toString();
String[] answers1 = getString(new ArrayList<Questions>manswer;
String correctanswer = answers1[QuestionNo];
//gets the answer and correct answer from the edit text and strings.xml respectively
correctanswer = correctanswer;
answer = answer;
if (answer.equals(correctanswer)) {
mScore++;
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonscorrect);
scoremm6.setText("Score: " + mScore);
enter_btn.setEnabled(false);
Toast.makeText(getApplicationContext(), "Correct.",
Toast.LENGTH_SHORT).show();
} else {
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonsincorrect);
enter_btn.setEnabled(false);
}
}
}
});
//我在这里也需要帮助才能转到另一个问题
Button Next2 = (Button) findViewById(R.id.next_btn);
Next2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String[] mquestions = getString(new ArrayList<Questions>);
if (QuestionNo < (mquestions.length -1)) {
QuestionNo = QuestionNo + 1;
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
enter_btn.setEnabled(true);
answer.getText().clear();
TextView t = (TextView) findViewById(R.id.question);
t.setText(mquestions[QuestionNo]);
}
{
if (QuestionNo == (questions.length)) {
enter_btn.setEnabled(false);
Toast.makeText(getApplicationContext(), "Test Completed",
Toast.LENGTH_LONG).show();
}
}
}
});
}
}
解决方案
你最终可以做到这一点
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private HashMap<String, Question> remainingQuestions;
private HashMap<String, Question> answeredQuestions;
private Question currentQuestion;
private int score = 0;
private TextView scoreField;
private TextView statement;
private EditText answer;
private Button checkAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreField = findViewById(R.id.score);
statement = findViewById(R.id.statement);
answer = findViewById(R.id.answer);
checkAnswer = findViewById(R.id.check_answer);
checkAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final String givenAnswer = answer.getText().toString();
if(givenAnswer.equals(currentQuestion.getAnswer())) {
remainingQuestions.remove(currentQuestion.getStatement());
answeredQuestions.put(currentQuestion.getStatement(), currentQuestion);
score++;
scoreField.setText(String.valueOf(score));
Toast.makeText(v.getContext(), "Good answer", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(), "Wrong answer", Toast.LENGTH_LONG).show();
}
if(remainingQuestions.size() != 0) {
askNewQuestion();
}
}
});
remainingQuestions = new HashMap<>();
answeredQuestions = new HashMap<>();
initQuestions();
}
@Override
protected void onPostResume() {
super.onPostResume();
askNewQuestion();
}
private void initQuestions() {
Log.d(TAG, "initQuestions");
remainingQuestions.put("2 x 3 ?", new Question("2 x 3 ?", "6"));
remainingQuestions.put("2 x 4 ?", new Question("2 x 4 ?", "8"));
remainingQuestions.put("2 x 5 ?", new Question("2 x 5 ?", "10"));
remainingQuestions.put("2 x 6 ?", new Question("2 x 6 ?", "12"));
remainingQuestions.put("2 x 7 ?", new Question("2 x 7 ?", "14"));
}
private void askNewQuestion() {
Log.d(TAG, "askNewQuestion");
final Random random = new Random();
currentQuestion = (Question) remainingQuestions.values().toArray()[random.nextInt(remainingQuestions.size())];
statement.setText(currentQuestion.getStatement());
answer.setText("");
}
}
推荐阅读
- javascript - 为什么 JS 不在“for in 循环”中强制 i 的值?
- ios - 使用 Agora Kit 进行语音通话
- javascript - 如何在有条件的情况下使用承诺中的方法?
- python-3.x - 如何在 Mac 上安装 python3
- arrays - 在 SSIS 中使用 JSONPath 表达式获取每个子节点及其值
- django - Django:在 sqlite3 的模型字段中存储字符串数组(文件路径)
- java - 如何使用正则表达式从字符串中捕获所有实例
- javascript - 如何根据另一个组件的状态更改 CSS 属性
- node.js - 注册节点/快递的身份验证时缺少用户名错误
- arrays - 为什么嵌套数组在python中复制其元素?