我在这里有一个构造函数类对象:
public class Question {
public String question;
public boolean correctAnswer;
public Question(String question, boolean correctAnswer)
{
this.question = question;
this.correctAnswer = correctAnswer;
}
}
在我的MainActivity中,我正在创建问题并将它们存储在List中。
然后我想在按钮单击时创建一个f / else语句,这样当用户单击按钮并且问题布尔类型与if语句匹配时,它会被提示另一个问题。问题是我不知道如何得到布尔类型的问题。
public class MainActivity extends ActionBarActivity {
Button mYes;
Button mNo;
TextView mQuestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int[] count = {0};
mYes = (Button)findViewById(R.id.button2);
mNo = (Button)findViewById(R.id.button);
mQuestion = (TextView)findViewById(R.id.textView);
//Creating question
final Question first = new Question("Do i understand this code?", false);
final Question second = new Question("kas tas?", true);
//question list
final ArrayList<Question> questions = new ArrayList<Question>();
//adding question
questions.add(first);
questions.add(second);
//show the question.
mQuestion.setText(questions.get(0).question);
mYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if () {
count[0]++;
mQuestion.setText(questions.get((count[0]) % questions.size()).question);
} else {
finish();
}
}
});
我可以通过编写first.correctAnswer来获得一个问题布尔类型。但是我需要所有的问题。我应该用for语句计算所有问题吗?如果是这样,我该如何从那个中扩展布尔类型?
答案 0 :(得分:0)
你应该创建一个布尔数组列表来保持答案,即true,false,true,true ......
ArrayList<boolean> answers = new ArrayList<boolean>();
由于你已经保留了问题的数量, 您应该匹配onClick事件列表中的答案......
if (answers.get(count[0])) { // if answer == true
count[0]++; // increase count
/// do whatever you want
}