在我的代码中,我在向上滑动时实现了myint ++,在向下滑动时实现了myint--但是,我想保持myint不超过某些值,即我不希望myint的值超过10或低于0.我无法找到如何实现这一点。任何帮助表示赞赏:)
int navX = navPref.getInt("navXPref", 5);
int navY = navPref.getInt("navYPref", 5);
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : navX++;
prefEditor.putInt("navXPref", navX);
prefEditor.apply();
break;
case SimpleGestureFilter.SWIPE_LEFT : navX--;
prefEditor.putInt("navXPref", navX);
prefEditor.apply();
break;
case SimpleGestureFilter.SWIPE_DOWN : navY--;
prefEditor.putInt("navYPref", navY);
prefEditor.apply();
break;
case SimpleGestureFilter.SWIPE_UP : navY++;
prefEditor.putInt("navYPref", navY);
prefEditor.apply();
break;
}
答案 0 :(得分:1)
为什么不做一个if语句?
case SimpleGestureFilter.SWIPE_DOWN : if(navY > 0)
navY--;
prefEditor.putInt("navYPref", navY);
prefEditor.apply();
break;
case SimpleGestureFilter.SWIPE_UP : if(navY < 10)
navY++;
prefEditor.putInt("navYPref", navY);
prefEditor.apply();
break;
答案 1 :(得分:0)
另一种解决方案:
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : navX++; break;
case SimpleGestureFilter.SWIPE_LEFT : navX--; break;
case SimpleGestureFilter.SWIPE_DOWN : navY--; break;
case SimpleGestureFilter.SWIPE_UP : navY++; break;
}
if (navX < 0) { navX = 0; }
if (navY < 0) { navY = 0; }
if (navX > 10) { navX = 10; }
if (navY > 10) { navY = 10; }
prefEditor.putInt("navXPref", navX);
prefEditor.apply();
prefEditor.putInt("navYPref", navY);
prefEditor.apply();