2017-05-08 14 views
-6

//主代碼我else if語句可在心不是我的井字程序工作

公共靜態無效的主要(字串[] 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 :("); 
     } 
+0

你的花括號中不以網格的方式設置你的預期的活動。如果和其他人應該在同一水平。 – Compass

+2

請更好格式化? 'do {}'? – Aryan

+2

什麼數據類型是'currentstate','you_win','draw'和'comp_win'?他們不是絃樂隊嗎? –

回答

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; 
} 
+0

哦,我明白了,非常感謝! – fluffytacoo

+0

@fluffytacoo很高興我可以幫助 - 你可以[接受這個答案](http://stackoverflow.com/help/accepted-answer)如果它解決了問題? – EJoshuaS