gpt4 book ai didi

android - Switch 语句无法正常运行

转载 作者:行者123 更新时间:2023-11-29 15:57:04 24 4
gpt4 key购买 nike

这里的目标是感知何时单击某个 View (通过单击的特定按钮标识的 ID 名称),然后在另一个类的实例中触发操作。

我不能 100% 确定它是否正确读取。我发布代码然后解释问题。

在主 Activity 中:

@Override
public void onClick(View v) {

//Set starting player to 1 or 0
if( mGame.getPlayerCounter() == 0 ) {
mGame.determineStartingPlayer();
}

//Fills buttons on mGame instance
fillButtonSpaces(v);
....

//Check if the board has to be reset for either a tie or a win
if(mGame.checkGameTie() || mGame.checkGameWinner()) {
//Resets java mGame board logic
mGame.resetGameBoard();

//Reset the physical GUI gameboard
resetPhysicalGameBoard();
}}

public void fillButtonSpaces(View v) {
switch(v.getId()){
case R.id.imageButtonOne :
mGame.fillSquareZero();
case R.id.imageButtonTwo :
mGame.fillSquareOne();
case R.id.imageButtonThree :
mGame.fillSquareTwo();
case R.id.imageButtonFour :
mGame.fillSquareThree();
case R.id.imageButtonFive :
mGame.fillSquareFour();
case R.id.imageButtonSix :
mGame.fillSquareFive();
case R.id.imageButtonSeven :
mGame.fillSquareSix();
case R.id.imageButtonEight :
mGame.fillSquareSeven();
case R.id.imageButtonNine :
mGame.fillSquareEight();
}
}

然后在另一个类中:

public void fillSquareZero() {
if( getCurrentPlayer() == 0) {
spaceZero = 0; } else {
spaceZero = 1;
}
}

public void fillSquareOne() {
if( getCurrentPlayer() == 0) {
spaceOne = 0; } else {
spaceOne = 1;
}
}

public void fillSquareTwo() {
if( getCurrentPlayer() == 0) {
spaceTwo = 0; } else {
spaceTwo = 1;
}
}
.... etc.....

应该调用这些方法来将游戏板的值设置为 1 或 0。然后我想检查这个方法(在每个 onclick 上调用以查看是否有赢家)

public boolean checkGameWinner() {
//Player 1 (Human) player winning rules
if (spaceZero == 0 && spaceOne == 0 && spaceTwo == 0) {
return true;
}

因此,根据单击哪个按钮(在开关盒中显示),mGame 实例在第二个类中将 spaceZero - spaceEight 变量设置为 1 或 0。所以我正在检查前 3 个是否设置为 0(玩家 1 选择了它们)。当我这样做时,我没有得到想要的结果,只要我点击第一个单元格,如果播放器 1 位图放在那里,板就会被清除......这是怎么回事?

最佳答案

您的 switch case 在每个 case 之后都缺少一个 break;。应该是

 switch(v.getId()){
case R.id.imageButtonOne :
mGame.fillSquareZero();
break;
case R.id.imageButtonTwo :
mGame.fillSquareOne();
break;
case R.id.imageButtonThree :
mGame.fillSquareTwo();
break;

等...

关于android - Switch 语句无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27474992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com