我有一个按钮,单击该按钮时应该进行计数,直到达到 ResetTouch 限制,然后重置为 0。
然而,事实并非如此。
int resetTouch = 4;
int number = 1;
int touches1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//adds number of touches
public void setTouches1 (View view){
if (touches1 < resetTouch){
touches1 = touches1 + number;
displayTeamTouches(touches1);
}
else{
touches1 = 0;
}
这种方法是有效的,但是不是在 3 处停止,而是转到 4。此时我必须单击 + 按钮两次才能将其重置为 1。我希望它增加到 3,然后重置为 0。
我做错了什么?
最佳答案
当您观察 touches1
时,它已经增加了。您的第一次运行看起来像 1、2、3、4,然后然后它将重置。
在递增变量之前,您应该观察该变量。这样,您实际上可以看到 0, 1, 2, 3。
if(touches1 < resetTouch) {
displayTeamTouches(touches1);
touches1 += number;
}
关于Java If 循环点击次数过多而无法重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50258680/