在Android Studio工作,我遇到了一些小问题,所以我想我会寻求帮助/建议。
我目前正在开发一个小型测试应用程序,允许用户打开应用程序并选择练习测试或定时测试(如图1所示)。如果用户选择练习测试,他们可以无限时间选择答案,系统会告诉他们是否选择了正确的答案。如果用户选择的时间比系统分配预定的时间量,并且在每个问题之后添加和计算分数,这就是我的问题。
1。)现在我的代码无休止地循环,理想情况下(至少在时间方面)我希望我的代码通过基于用户选择得分正确或不正确的5个问题。然后我想显示#correct和正确的。
所以我的问题是双重的,首先我如何防止我的代码循环无休止,所以我可以开始记录迭代为++ correctAnswer ++错误的答案。而且,当需要显示结果时,我应该创建一个新活动来显示结果。
Android对我来说有点混乱,我希望我提供了足够的信息,我很感激并提供建议。
谢谢
**代码**
private int currentQuestion;
private String[] questions;
private String[] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
questions = new String[]{"What is the acronym for Computer Science and Information Management?",
"What is the CSIT corse number of the class you are currently in?",
"What Android program do we use to create android applications?"};
answers = new String[]{"CSIT","2250", "Android Studio"};
currentQuestion = -1;
answerButton = (Button)findViewById(R.id.AnswerButton);
questionButton = (Button)findViewById(R.id.QuestionButton);
questionView = (TextView)
findViewById(R.id.QuestionTextView);
answerView = (TextView) findViewById(R.id.AnswerTextView);
answerText = (EditText) findViewById(R.id.AnswerText);
answerButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
checkAnswer();
}});
questionButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
showQuestion();
}});
}
// Display current quesiton
public void showQuestion()
{
currentQuestion++;
if(currentQuestion == questions.length)
currentQuestion =0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
}
// Return true if answer is correct
public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(answers[currentQuestion]));
}
// Display correct or incorrec results
public void checkAnswer()
{
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You are correct!");
else
answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}}
答案 0 :(得分:0)
只需在currentQuestion == 5
结束时检查checkAnswer()
是否有条件。如果有,请继续进入结果屏幕