gpt4 book ai didi

android - android studio中的无限循环问题

转载 作者:行者123 更新时间:2023-12-03 18:13:34 24 4
gpt4 key购买 nike

在 Android Studio 工作,我遇到了一些小问题,所以我想我会寻求帮助/建议。

我目前正在开发一个小型测试应用程序,它允许用户打开应用程序并选择练习测试或定时测试(如图 1 所示)。如果用户选择练习测试,他们有无限的时间来选择答案,系统会告诉他们他们是否选择了正确的答案。如果用户选择计时,系统会分配预定的时间,并且在每个问题之后添加和计算分数,这就是我的问题。

1.)现在我的代码无休止地循环,理想情况下(至少在定时方面)我希望我的代码通过 5 个问题,根据用户的选择对正确或错误进行评分。然后我想显示正确和正确的#。

所以我的问题有两个,首先我如何防止我的代码无限循环,这样我就可以开始将迭代记录为++correctAnswer++incorrect answer。而且,当需要显示结果时,我是否应该创建一个新 Activity 来显示结果。

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]);

}}

Home Screen
主屏幕

enter image description here
测验屏幕

最佳答案

只要有条件检查 currentQuestion == 5checkAnswer() 结尾.如果是这样,请进入结果屏幕

关于android - android studio中的无限循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34013238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com