Android:随机数决定玩家的回合

时间:2013-08-28 09:15:53

标签: java android random numbers generator

我想创建1到2之间的随机整数(对于player1和player2)。然后,如果它是1,则玩家1应该赢得第一个移动,如果是2,则玩家2应该赢得第一个移动。我尝试过类似的东西,但它不起作用:

Random generator = new Random();
int rand = generator.nextInt(2) + 1;

if(rand == 1){
    player1 = true;
    player2 = false;
    Toast.makeText(getApplicationContext(), playerOneName + " won the first move!", Toast.LENGTH_SHORT);
} else {
    player1 = false;
    player2 = true;
    Toast.makeText(getApplicationContext(), playerTwoName + " won the first move!", Toast.LENGTH_SHORT);
}

它不会出错,但没有任何反应。总是玩家1进行第一步,而吐司不会出现!

2 个答案:

答案 0 :(得分:3)

首先注意你的toast命令没有完成,应该是:

 Toast.makeText(getApplicationContext(), playerOneName + " won the first move!", Toast.LENGTH_SHORT).show();

然后你会更好地了解什么是

答案 1 :(得分:1)

要制作Toast节目,您需要在创建后添加.show(),即

替换

Toast.makeText(getApplicationContext(), playerOneName + " won the first move!", Toast.LENGTH_SHORT);

Toast.makeText(getApplicationContext(), playerOneName + " won the first move!", Toast.LENGTH_SHORT).show();