java - 方法跳过以执行一些代码
问题描述
代码
private void updateQuestion() {
mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child("Question").getValue().toString();
answer = dataSnapshot.child("Answer").getValue().toString();
option1 = dataSnapshot.child("Option1").getValue().toString();
option2 = dataSnapshot.child("Option2").getValue().toString();
option3 = dataSnapshot.child("Option3").getValue().toString();
option4 = dataSnapshot.child("Option4").getValue().toString();
que.setText(question);
opt1.setText(option1);
opt2.setText(option2);
opt3.setText(option3);
opt4.setText(option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option2.equals(answer)) {
opt2.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt2.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt2.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt2.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option3.equals(answer)) {
opt3.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt3.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt3.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt3.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option4.equals(answer)) {
opt4.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt4.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt4.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt4.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
}
}
它的行为很奇怪。当我得到第一个问题并且我选择了错误的答案时,它会毫无问题地进入下一个问题。但是,当我选择正确答案时,它会跳过下一个问题并转到之后的问题。例如,如果我在问题 2 中选择正确答案,它会跳过问题 3 并显示问题 4。这很奇怪,我无法弄清楚我哪里出错了。
我确定我的 if 语句中有一些错误,但无法弄清楚它是什么。
解决方案
在您的两个处理程序中,您正在调用
mQuestionNumber++;
updateQuestion();
如果答案正确,则呼叫两次
if(correct){
handler //once
}else{
}
handler //twice
解决方案是删除first //once
处理程序
IE
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
从if condition
更新为opt1
opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
推荐阅读
- python - python中数百万行的高效欧几里得距离计算
- python - 为什么我的自定义损失函数不接受转换后的 numpy 数组?
- python - 如何在 pandas 创建的条形图上获取 x-ticks 的位置?
- javascript - 部分客户端更新 Chrome 后无法获取 IP
- angularjs - 使用 angular.copy
- symfony - [主键缺失值]
- deep-learning - Google Colab 中的 Yolo V3
- python - python - 如何使用模来查找一个数字是否可以被python中的第二个整除?
- php - 正则表达式在任何版本的 PHP 中都返回 false
- android - 如何在RelativeLayout Android上添加工具栏