------- <a target="blank">android培训></a>、<a target="blank">java培训</a>、期待与您交流! ----------
private int up=0,dawn=1,left=2,right=3,Direction=0;
switch (Direction) {
case up:
canvas.drawLine(x, y, x, y+r, paint);
break;
case dawn:
canvas.drawLine(x, y, x, y-r, paint);
break;
case left:
canvas.drawLine(x, y, x-r, y, paint);
break;
case right:
canvas.drawLine(x, y, x+r, y, paint);
break;
} 在case中报错了,这是为什么?错误提示case表达式必须是常量表达式
case必须跟常量,不能跟变量,这是switch...case语句的要求
补充:
改为下面的,就可以了~
private int up=0,dawn=1,left=2,right=3,Direction=0;
switch (Direction) {
case 0:
canvas.drawLine(x, y, x, y+r, paint);
break;
case 1:
canvas.drawLine(x, y, x, y-r, paint);
break;
case 2:
canvas.drawLine(x, y, x-r, y, paint);
break;
case 3:
canvas.drawLine(x, y, x+r, y, paint);
break;
}