条件永不返回真
Condition never returning true
我有一个 if 语句来检查三个插槽是否完成。如果是,计时器应该停止,但由于某种原因代码不是 运行。我见过一个 post 类似于这个 If-condition never executes despite correct condition,但是他们的解决方案没有解决任何问题。
这是我的代码:
停止函数
public void stop(ImageSwitcher slot){
slotOneFinished = (slot.equals(slotOne));
slotTwoFinished = (slot.equals(slotTwo));
slotThreeFinished = (slot.equals(slotThree));
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished){
//not running
Toast.makeText(MainActivity.this, "Running",Toast.LENGTH_SHORT).show();
checkWin(getFruits());
timer.cancel();
timer = null;
}
}
定时器
private Timer timer;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new TimerTask() {
@Override
public void run() {
if (!slotOneFinished){
animate(randomSwitchCount(), slotOne);
}
if (!slotTwoFinished) {
animate(randomSwitchCount(), slotTwo);
}
if (!slotThreeFinished) {
animate(randomSwitchCount(), slotThree);
}
}
});
}
};
动画函数
public void animate(final int maxCount, final ImageSwitcher slot) {
i++;
if (i<maxCount){
Animation in = AnimationUtils.loadAnimation(this, R.anim.new_slot_item_in);
Animation out = AnimationUtils.loadAnimation(this, R.anim.old_item_out);
slot.setInAnimation(in);
slot.setOutAnimation(out);
int fruit = randomFruit();
slot.setTag(fruit);
slot.setImageResource(fruit);
}else {
stop(slot);
}
}
使用 == 也没有任何作用。
感谢您的帮助,
PiNet
这个条件永远不可能成立,假设 equals()
是以规范方式实现的,并且 slotOne
、slotTwo
和 slotThree
是 3 个不同的对象:
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished)
看来您对变量范围的假设有误。您可能可以通过使用这样的条件来解决此问题,而不是:
if( slot == slotOne )
slotOneFinished = true;
...等等。
Android Studio 调试器是您的朋友。
我有一个 if 语句来检查三个插槽是否完成。如果是,计时器应该停止,但由于某种原因代码不是 运行。我见过一个 post 类似于这个 If-condition never executes despite correct condition,但是他们的解决方案没有解决任何问题。
这是我的代码:
停止函数
public void stop(ImageSwitcher slot){
slotOneFinished = (slot.equals(slotOne));
slotTwoFinished = (slot.equals(slotTwo));
slotThreeFinished = (slot.equals(slotThree));
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished){
//not running
Toast.makeText(MainActivity.this, "Running",Toast.LENGTH_SHORT).show();
checkWin(getFruits());
timer.cancel();
timer = null;
}
}
定时器
private Timer timer;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new TimerTask() {
@Override
public void run() {
if (!slotOneFinished){
animate(randomSwitchCount(), slotOne);
}
if (!slotTwoFinished) {
animate(randomSwitchCount(), slotTwo);
}
if (!slotThreeFinished) {
animate(randomSwitchCount(), slotThree);
}
}
});
}
};
动画函数
public void animate(final int maxCount, final ImageSwitcher slot) {
i++;
if (i<maxCount){
Animation in = AnimationUtils.loadAnimation(this, R.anim.new_slot_item_in);
Animation out = AnimationUtils.loadAnimation(this, R.anim.old_item_out);
slot.setInAnimation(in);
slot.setOutAnimation(out);
int fruit = randomFruit();
slot.setTag(fruit);
slot.setImageResource(fruit);
}else {
stop(slot);
}
}
使用 == 也没有任何作用。
感谢您的帮助,
PiNet
这个条件永远不可能成立,假设 equals()
是以规范方式实现的,并且 slotOne
、slotTwo
和 slotThree
是 3 个不同的对象:
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished)
看来您对变量范围的假设有误。您可能可以通过使用这样的条件来解决此问题,而不是:
if( slot == slotOne )
slotOneFinished = true;
...等等。
Android Studio 调试器是您的朋友。