2016-05-24 164 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