我对OOP很新。我终于慢慢地把握了它的概念。但我仍然坚持如何抓住下一个新的"盒子#34;来自我的测验课的类型问题。我甚至不知道如何解释它,可能缺少适当的术语。但这是我的代码。
我有两个课程:问题和测验
public class Question{
String question;
public Question(String placeQuestion){
question = placeQuestion;
}
}
public class Quiz{
private ArrayList<Question> questions = new ArrayList<Question>();
public void setQuestion(String q){
Question setQ = new Question(q);
questions.add(setQ);
//each time here I create a "box" of Question class that has a String question in it
}
public Question getNextQuestion(){
//here I want to get the next "box" Question class with the String question
//in it. How do I track the current question "box" and go to the next one?
}
}
那么我该如何进入下一个问题类&#34;框&#34;在测验班?例如,我有20个问题。我想让getNextQuestion返回问题&#34;框&#34;问题2,如果我在1。或者实际上我怎么知道我在问题1&#34;框&#34;
答案 0 :(得分:0)
这样的事情:
public class Question {
String question;
public Question(String placeQuestion) {
question = q;
}
}
public class Quiz {
private ArrayList<Question> questions = new ArrayList<Question>();
private int qindex=0;
public void setQuestion(String q){
Question setQ = new Question(q);
questions.add(setQ);
//each time here I create a "box" of Question class that has a String question in it
}
public Question getNextQuestion(){
Question q = questions.get(qindex);
qindex++;
return q;
}
}
请注意,您需要做更多的工作 - 比如在调用问题[qindex]之前检查ArrayList的长度。