在我的应用程序中,我有一个随机挑选的图像。图像出现在屏幕上,用户可以将其滑动到一侧。每次它是一个图像a
,计数器应计数,但它不。我的int getMyRandomResId的返回值在条件中是错误的,还是错误在哪里?android - switch if case语句的返回值
public class MainActivity extends Activity
{
private ImageView imageView;
public int sco = 0;
float x1, x2;
float y1, y2;
final Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
score = (TextView)findViewById(R.id.textView1);
score.setText(String.valueOf(sco));
}
int getMyRandomResId()
{
int imag = rand.nextInt(4);
switch (imag)
{
case 0:
return R.drawable.a;
case 1:
return R.drawable.b;
case 2:
return R.drawable.c;
default:
return R.drawable.d;
}
}
public boolean onTouchEvent(MotionEvent touchevent)
{
final ViewPropertyAnimator animator = imageView.animate();
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2 && (x2 - x1) >= (y1 - y2) && (x2 - x1) >= (y2 - y1))
{
if(getMyRandomResId() == R.drawable.a)
{
sco++;
score.setText(String.valueOf(sco));
}
animationRunning = true;
animator.translationX((imageView.getWidth()))
.setDuration(250)
.setListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animation)
{
imageView.setTranslationX(0);
imageView.setImageResource(getMyRandomResId());
animationRunning = false;
}
})
.start();
}
}
}
}
因为我感到很困惑的答案@StefMa指着我,我要在这里问你一件事:与调试器,你尝试检查,如果如果条件是到达?尝试在你的getMyRandomId变量的结果和调试器检查它的值 –
我猜你的代码是正确的。但不幸的是(因为随机int),你还没有达到这一点。我创建了一个测试设置,它是绿色的。这意味着一切都是正确的...请参阅https://git.io/v1Hrw – StefMa
@StefMa那么你知道它为什么不起作用吗? –