对于我的新游戏,我需要一个显示当前分数的计数器。我有四种形状,这些形状填充了随机选择的列表中的颜色。如果第一个形状用红色填充,则计数器应该增加,否则不应该增加。我试图实现它,但计数器不计算,我不知道是什么错。
public class GameScreen extends Activity implements OnGestureListener{
private Paint paint = new Paint();
public int score = 0;
TextView text;
public int colora;
public int colorb;
public int colorc;
public int colord;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
List<Integer> numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
Collections.shuffle(numbers);
colora = numbers.get(0);
colorb = numbers.get(1);
colorc = numbers.get(2);
colord = numbers.get(3);
if(colora == Color.RED){
score = score++;
}
TextView score = (TextView)findViewById(R.id.textView1);
score.setText(String.valueOf(score));
}
}
答案 0 :(得分:1)
x++
运算符递增变量的值,然后返回增量前的值。因此,当您编写score = score++
时,您正在递增score
,然后立即用增量前的值覆盖该值,这意味着它将始终为零。
答案 1 :(得分:0)
如果你想在你输入的变量中加1:score ++;或得分=得分+ 1; not score = score ++;