我的其他if语句不适用于我的tic tac toe程序

时间:2017-05-08 19:27:03

标签: java tic-tac-toe

//主要代码

public static void main(String [] args){

//启动游戏板

initGame();
    //start the game
    do {
        PlayerMove(Cplayer);
        updategame(Cplayer, crow, ccol);
        printboard();
        //game over message
        if (currentstate == you_win){
            System.out.println("'X' Won!!");
            else if (currentstate == comp_win)
                System.out.println("'O' won!!");
                else if (currentstate == draw)
                System.out.println("It's a Draw :(");
        }

1 个答案:

答案 0 :(得分:0)

if (currentstate == you_win){
        System.out.println("'X' Won!!");
        else if (currentstate == comp_win)
            System.out.println("'O' won!!");
            else if (currentstate == draw)
            System.out.println("It's a Draw :(");
    }

看看这一行(我删除了一些无关的代码,以便更容易看到):

    if (currentstate == you_win){
        else if (currentstate == comp_win)
            //...
    }

以这种方式看,你能看出为什么这是不正确的吗?顺便说一句,这就是您应该始终使用{}的原因。请改为if ... else

if (currentstate == you_win){
      // ...
 }
 else if (currentstate == comp_win) {
     // ...
  }
    //...

或者,更好的是,只需使用switch声明:

switch (currentstate) {
       case you_win:
             System.out.println("'X' Won!!");
             break;
        case comp_win:
            System.out.println("'O' won!!");
            break;

        case draw:
            System.out.println("It's a Draw :(");
            break;
}