在带有 ifs 的嵌套 for 循环中,我的逻辑错误在哪里?
Where is the error in my logic in nested for-loops with ifs?
我编写了某种测验。首先,用户可以提问并回答。这些保存在 question/answer-table.
如果这个table中有足够的条目,就可以开始小测验了。测验遍历 question/answer-table 中的条目,随机选择一个问题并向用户提问。他的答案保存在一个测验中-table.
测验中的条目-table 将当前用户考虑在内,因此如果已经有一个用户的条目(也许他之前已经被问过这个问题),那么答案会更新条目在测验中-table。如果用户之前被问过这个问题,则会在测验中创建一个新条目-table。
其中大部分或多或少都有效,但不知何故,测验中的条目 - table 没有更新,我不明白为什么。那么我的逻辑错误在哪里呢?
//Creates a list.The user answers the question via radiobuttons.
//All quiz-entries that match the question_ID get put into the list.
//Bob and Tim could have answere the same question,
//so the question_ID could be in there several times
List<Quiz> tempQuizList = Quiz.find.where().like("question_ID", clickedRadioAnswer.questionID).findList();
User currentUser = request().name();
if( tempQuizList.size() > 0 ){
// Go through all entries in the quiz-table
for (Quiz quizItem : Quiz.find.all()) {
// If the user from the quiz-table entry is the same as the current user
if((quizItem.userID).equals(currentUser.email)){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
// If user has answered correctly, update the entry with the user and an interval to postpone the time when the user has to answer the question again
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
}
else{
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
}
}
if(!(quizItem.userID).equals(currentUser.email)){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.createAnswer(clickedRadioAnswer, currentUser, 5000);
}
else{
Quiz.createAnswer(clickedRadioAnswer, currentUser, 0);
}
}
}
}
我找到了一个非常肮脏的解决方案:
在我的for循环中,我问,如果数据库中问题的用户与当前登录的用户相同。如果我将另一个.like()
添加到我的列表中,我的结构变得更简单:
List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID).findList();
变成:
List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID)
.like("user_id", currentUser.email).findList();
有了这个,我的大 if 块的第二部分就不再需要了,因为 100% 的用户是当前用户(否则问题不会被放入列表中):
if( tempQuizList.size() > 0 ){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
}
if(!clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
}
}
// Quiz is empty or user has no quizquestion open
if(tempQuizList.size() == 0){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 5000);
}
else{
Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 0);
}
我尝试使用调试器,但发现实际上很难看出错误在哪里。相反,我通过 Skype 与一位朋友通话并向他解释了我在做什么。然后他指出,for 循环太多了。我们一起找到了 double-like 的解决方案。它看起来不漂亮,但完成了工作。
我编写了某种测验。首先,用户可以提问并回答。这些保存在 question/answer-table.
如果这个table中有足够的条目,就可以开始小测验了。测验遍历 question/answer-table 中的条目,随机选择一个问题并向用户提问。他的答案保存在一个测验中-table.
测验中的条目-table 将当前用户考虑在内,因此如果已经有一个用户的条目(也许他之前已经被问过这个问题),那么答案会更新条目在测验中-table。如果用户之前被问过这个问题,则会在测验中创建一个新条目-table。
其中大部分或多或少都有效,但不知何故,测验中的条目 - table 没有更新,我不明白为什么。那么我的逻辑错误在哪里呢?
//Creates a list.The user answers the question via radiobuttons.
//All quiz-entries that match the question_ID get put into the list.
//Bob and Tim could have answere the same question,
//so the question_ID could be in there several times
List<Quiz> tempQuizList = Quiz.find.where().like("question_ID", clickedRadioAnswer.questionID).findList();
User currentUser = request().name();
if( tempQuizList.size() > 0 ){
// Go through all entries in the quiz-table
for (Quiz quizItem : Quiz.find.all()) {
// If the user from the quiz-table entry is the same as the current user
if((quizItem.userID).equals(currentUser.email)){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
// If user has answered correctly, update the entry with the user and an interval to postpone the time when the user has to answer the question again
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
}
else{
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
}
}
if(!(quizItem.userID).equals(currentUser.email)){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.createAnswer(clickedRadioAnswer, currentUser, 5000);
}
else{
Quiz.createAnswer(clickedRadioAnswer, currentUser, 0);
}
}
}
}
我找到了一个非常肮脏的解决方案:
在我的for循环中,我问,如果数据库中问题的用户与当前登录的用户相同。如果我将另一个.like()
添加到我的列表中,我的结构变得更简单:
List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID).findList();
变成:
List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID)
.like("user_id", currentUser.email).findList();
有了这个,我的大 if 块的第二部分就不再需要了,因为 100% 的用户是当前用户(否则问题不会被放入列表中):
if( tempQuizList.size() > 0 ){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
}
if(!clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
}
}
// Quiz is empty or user has no quizquestion open
if(tempQuizList.size() == 0){
if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 5000);
}
else{
Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 0);
}
我尝试使用调试器,但发现实际上很难看出错误在哪里。相反,我通过 Skype 与一位朋友通话并向他解释了我在做什么。然后他指出,for 循环太多了。我们一起找到了 double-like 的解决方案。它看起来不漂亮,但完成了工作。