我正在创建一个生成问题的java应用程序,下面显示的方法用于生成问题。我已经使用堆栈来按顺序排列这些问题。 我的问题是它不断产生多个相同的问题,这个方法应该阻止。
public ArrayList<String> generateQuestions(String transcript, String className, int classYear, String classGroup) {
ArrayList<String> results = new ArrayList<String>();
ArrayList<String> tags = new ArrayList<String>();
//get tags from the database
Statement stmt = null;
try {
stmt = handler.getConnection().createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
if(stmt != null){
try {
String result = "";
ResultSet rs = stmt.executeQuery("SELECT tags.TagName FROM class, tags, questtag, questions, questclass WHERE questtag.QuestionID = questions.QuestionID AND tags.TagID = questtag.TagID AND questions.QuestionID = questclass.QuestionID AND questclass.ClassID = class.ClassID AND class.ClassName ='"+className+"' AND class.ClassYear="+classYear+" AND class.ClassGroup ='"+classGroup+"' GROUP BY tags.TagID");
while (rs.next()) {
result = rs.getString(1);
tags.add(result);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
Stack<Question> stack = new Stack<Question>();
for (String word : transcript.split("\\s+")) {
if( ! word.equalsIgnoreCase("the") || ! word.equalsIgnoreCase("I")) {
for(String tag : tags) {
if(word.equalsIgnoreCase(tag)) {
try {
stmt = handler.getConnection().createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
if(stmt != null){
try {
String result = "";
ResultSet rs = stmt.executeQuery("SELECT questions.Question FROM class, tags, questtag, questions, questclass WHERE questtag.QuestionID = questions.QuestionID AND tags.TagID = questtag.TagID AND questions.QuestionID = questclass.QuestionID AND questclass.ClassID = class.ClassID AND class.ClassName ='"+className+"' AND class.ClassYear="+classYear+" AND class.ClassGroup ='"+classGroup+"' AND tags.TagName = '"+tag+"'");
while (rs.next()) {
result = rs.getString(1);
Stack<Question> searchStack = new Stack<Question>();
boolean multiple = false;
if(stack.isEmpty()) { //1st question we've come to
Question question = new Question(result);
stack.push(question);
} else {
while(multiple == false && !stack.isEmpty()) {
//search through the stack
searchStack.push(stack.pop());
//if question is not already in the stack
if(((Question)searchStack.peek()).getQuestion().equalsIgnoreCase(result)) {
//then it is multiple
//add one to its noOfTimes
((Question)searchStack.peek()).addToNoOfTimes();
//order this above others with lower score
if(!stack.isEmpty()) {
if(((Question)stack.peek()).getNoOfTimes() < ((Question)searchStack.peek()).getNoOfTimes()) {
Question thisQuestion = searchStack.pop();
while(((Question)stack.peek()).getNoOfTimes() < thisQuestion.getNoOfTimes() && !stack.isEmpty()) {
searchStack.push(stack.pop());
}
stack.push(thisQuestion);
} else {
stack.push(searchStack.pop());
}
}
multiple = true;
}
}
while(!searchStack.empty())
stack.push(searchStack.pop());
if(multiple == false) {
Question question = new Question(result);
stack.push(question);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
Stack<Question> reorderStack = new Stack<Question>();
while(!stack.empty()) {
reorderStack.push(stack.pop());
}
while(!reorderStack.empty()) {
Question thisQuestion = reorderStack.pop();
results.add(thisQuestion.getQuestion());
stack.push(thisQuestion);
}
}
}
}
}
}
return results;
}