2016-05-24 220 views
1

任何人都可以幫助我將此do-while循環轉換爲while循環嗎?我試過,但似乎無法切換沒有很多錯誤。謝謝!在tic tac toe遊戲中將do while while循環更改爲while循環

public static void main(String args[]) { 
    String ch; 
    TicTacToe Toe = new TicTacToe(); 
    do { 
     Toe.newBoard(); 
     Toe.play(); 
     System.out.println("Would you like to play again (Enter 'yes')? "); 
     Scanner in = new Scanner(System.in); 
     ch = in.nextLine(); 
     System.out.println("ch value is " + ch); 
    } while (ch.equals("yes")); 
} 

這裏的井字棋遊戲控制檯的完整代碼: 進口java.util.Scanner的;

public class TicTacToe { 
    private int tic; 
    private char tac[] = new char[10]; 
    private char player; 
    public static void main(String args[]) { 
     String ch; 
     TicTacToe Toe = new TicTacToe(); 
     do { 
      Toe.newBoard(); 
      Toe.play(); 
      System.out.println("Would you like to play again (Enter 'yes')? "); 
      Scanner in = new Scanner(System.in); 
      ch = in.nextLine(); 
      System.out.println("ch value is " + ch); 
     } while (ch.equals("yes")); 
    } 
    public void newBoard() { 
     char posndef[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; 
     int i; 
     tic = 0; 
     player = 'X'; 
     for (i = 1; i < 10; i++) 
      tac[i] = posndef[i]; 
     currentBoard(); 
    } 
    public String currentBoard() { 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[1] + " | " + tac[2] + " | " + tac[3]); 
     System.out.println(" \t\t ____|____|____ "); 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[4] + " | " + tac[5] + " | " + tac[6]); 
     System.out.println(" \t\t ____|____|____ "); 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[7] + " | " + tac[8] + " | " + tac[9]); 
     System.out.println(" \t\t  | | "); 
     return "currentBoard"; 
    } 
    public void play() { 
     int spot; 
     char blank = ' '; 
     System.out.println("Player " + getPlayer() + " will go first and be the letter 'X'"); 
     do { 
      System.out.println("\n\n Player " + getPlayer() + " choose a position."); 
      boolean posTaken = true; 
      while (posTaken) { 
       Scanner in = new Scanner(System.in); 
       spot = in.nextInt(); 
       posTaken = checkPosn(spot); 
       if (posTaken == false) 
        tac[spot] = getPlayer(); 
      } 
      System.out.println("Nice move."); 
      currentBoard(); 
      nextPlayer(); 
     } while (checkWinner() == blank); 
    } 
    public char checkWinner() { 
     char Winner = ' '; 
     if (tac[1] == 'X' && tac[2] == 'X' && tac[3] == 'X') 
      Winner = 'X'; 
     if (tac[4] == 'X' && tac[5] == 'X' && tac[6] == 'X') 
      Winner = 'X'; 
     if (tac[7] == 'X' && tac[8] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[1] == 'X' && tac[4] == 'X' && tac[7] == 'X') 
      Winner = 'X'; 
     if (tac[2] == 'X' && tac[5] == 'X' && tac[8] == 'X') 
      Winner = 'X'; 
     if (tac[3] == 'X' && tac[6] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[1] == 'X' && tac[5] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[3] == 'X' && tac[5] == 'X' && tac[7] == 'X') 
      Winner = 'X'; 
     if (Winner == 'X') { 
      System.out.println("Player1 wins the game."); 
      return Winner; 
     } 
     if (tac[1] == 'O' && tac[2] == 'O' && tac[3] == 'O') 
      Winner = 'O'; 
     if (tac[4] == 'O' && tac[5] == 'O' && tac[6] == 'O') 
      Winner = 'O'; 
     if (tac[7] == 'O' && tac[8] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[1] == 'O' && tac[4] == 'O' && tac[7] == 'O') 
      Winner = 'O'; 
     if (tac[2] == 'O' && tac[5] == 'O' && tac[8] == 'O') 
      Winner = 'O'; 
     if (tac[3] == 'O' && tac[6] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[1] == 'O' && tac[5] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[3] == 'O' && tac[5] == 'O' && tac[7] == 'O') 
      Winner = 'O'; 
     if (Winner == 'O') { 
      System.out.println("Player2 wins the game."); 
      return Winner; 
     } 
     // check for Tie 
     for (int i = 1; i < 10; i++) { 
      if (tac[i] == 'X' || tac[i] == 'O') { 
       if (i == 9) { 
        char Draw = 'D'; 
        System.out.println(" Tied Game "); 
        return Draw; 
       } 
       continue; 
      } else 
       break; 
     } 
     return Winner; 
    } 
    public boolean checkPosn(int spot) { 
     if (tac[spot] == 'X' || tac[spot] == 'O') { 
      System.out.println("That position is already taken, please choose another"); 
      return true; 
     } else { 
      return false; 
     } 
    } 
    public void nextPlayer() { 
     if (player == 'X') 
      player = 'O'; 
     else 
      player = 'X'; 
    } 
    public String getTitle() { 
     return "Tic Tac Toe"; 
    } 
    public char getPlayer() { 
     return player; 
    } 
} 
+0

向我們展示你嘗試過什麼的'while'循環。否則我們不知道你在做什麼錯 – Hill

+0

public static void main(String args []){ \t \t String ch; \t \t TicTacToe Toe = new TicTacToe(); (ch.equals(「yes」)){ \t \t \t Toe.newBoard(); \t \t \t Toe.play(); System.out.println(「你想再玩一次(輸入'yes')?」); \t \t \t Scanner in = new Scanner(System.in); \t \t \t ch = in.nextLine(); \t \t \t System.out.println(「ch value is」+ ch); \t \t} \t} – Alex

+0

我不知道做什麼用CH – Alex

回答

3

不知道爲什麼你要做到這一點,但是它作爲使while條件通達到循環時一樣簡單:

String ch = "yes"; 
TicTacToe Toe = new TicTacToe(); 
while("yes".equalsIgnoreCase(ch)) { 
    Toe.newBoard(); 
    Toe.play(); 
    System.out.println("Would you like to play again (Enter 'yes')? "); 
    Scanner in = new Scanner(System.in); 
    ch = in.nextLine(); 
} 
+1

釘在頭上。詳細說明一下,你將'ch'(在你的情況下未初始化)與''yes''比較失敗。你需要初始化你的循環條件,使它至少通過一次。 – Hill

+0

感謝您的幫助! – Alex

+0

爲了迴應Hill的評論,我編輯了我的答案,以顯示基於字符串使用'equalsIgnoreCase'的慣例。當使用'ch.equals'時,你可能會遇到'NullPointerException',因爲它沒有被初始化。 – Zircon