我正在开发一个简单的 android 骰子游戏,其中 2 名玩家轮流滚动,如果你得到双打你就赢了。我正在尝试使用 toString 输出到当玩家在双打时获胜的祝酒词。
这是我的代码:
public class MainActivity extends AppCompatActivity {
public static final Random RANDOM = new Random();
private Button rollDice;
int player = 1;
private ImageView dice1, dice2;
TextView display;
boolean rolling = false;
@NonNull
public String toString(){
return String.valueOf(player);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollDice = (Button) findViewById(R.id.rollDice);
dice1 = (ImageView) findViewById(R.id.dice1);
dice2 = (ImageView) findViewById(R.id.dice2);
display = (TextView) findViewById(R.id.display);
其次是我的 onclick 方法和 random.nextInt 和我的开关案例。
然后我在 toast 中调用 toString:
if (d1 == d2){
Context context = getApplicationContext();
String text = "Player " + toString() + " wins!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
if (player == 1){
player = 2;
}
else{
player = 1;
}
然后它输出“玩家 com.ellis.newdice.MainActivity$1然后随机数获胜”
我是错误地使用了 toString 方法还是 int 播放器没有正确使用?
青春有我
繁花如伊
慕丝7291255
慕盖茨4494581
慕容708150
随时随地看视频慕课网APP
相关分类