简体   繁体   English

java帮助循环主代码

[英]java assistance for looping the main code

Im trying to write a code and i used the do while loop in an atempt to loop my main code after the user types in y or yes (case insensitive) into an InputDialog. 我试图编写代码,并且在用户输入y或yes(不区分大小写)到InputDialog之后,尝试使用do while循环来循环我的主代码。

here is the code, the only error is that a } needs to be put in after the do { but eve if i put it into the place I want, it doesn't work, also, it does not recognize thw while statement at the bottom, please help. 这是代码,唯一的错误是在do {之后需要插入一个},但是如果我将它放到我想要的位置,它将无法正常工作,而且,即使在底部,请帮助。

package tictactoemain;

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

/**
* JFrame to hold TicTacToe board.
*/
public class TicTacToeFrame extends JFrame
{     

    **do {** //do starts here
    private static final long serialVersionUID = 1L;

   // Indicate whose turn it is
   private char whoseTurn = 'X';
   private boolean gameOver = false;

   // Create cell grid using an Array
   private Cell[][] cells = new Cell[3][3];

   // Create a status label
   JLabel jlblStatus = new JLabel("X's turn to play");

   /**
    * No-argument Constructor
    * @return 
    */
   public TicTacToeFrame()
   {
       // Panel to hold cells
       JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               panel.add(cells[i][j] = new Cell());

       panel.setBorder(new LineBorder(Color.black, 1));
       jlblStatus.setBorder(new LineBorder(Color.black, 1));

       add(panel, BorderLayout.CENTER);
       add(jlblStatus, BorderLayout.SOUTH);
   }
   // Determine if it's a tie
   /**
    * Determine if game board is full.
    * @return True, if game board is full. Otherwise, false.
    */
    public boolean isFull()
    {
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               if (cells[i][j].getToken() == ' ')
                   return false;
       return true;
    }
    // Check to see if a player (Token) has won
   /**
    * Determines if a given token has won.
    * @param token Token to test for winning
    * @return True, if the token has won. Otherwise, false.
    */
   public boolean isWon(char token)
   {
       // check rows
       for (int i = 0; i < 3; i++)
           if ((cells[i][0].getToken() == token)
                   && (cells[i][1].getToken() == token)
                   && (cells[i][2].getToken() == token))
           {
               return true;
           }

       // check columns
       for (int j = 0; j < 3; j++)
           if ((cells[0][j].getToken() == token)
               && (cells[1][j].getToken() == token)
               && (cells[2][j].getToken() == token))
           {
               return true;
           }
       // check diagonals
       if ((cells[0][0].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][2].getToken() == token))
           {
               return true;
           }

       if ((cells[0][2].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][0].getToken() == token))
           {
               return true;
           }

       return false;
   }

    /**
    * Defines a cell in a TicTacToe game board.
    */
    public class Cell extends JPanel
    {
       /**
         * 
         */
        private static final long serialVersionUID = 1L;
    // token of this cell
       private char token = ' ';

       /**
        * Constructor
        */
       public Cell()
       {
           setBorder(new LineBorder(Color.black, 1));
           addMouseListener(new MyMouseListener());
       }

       /**
        * Gets the token of the cell.
        * @return The token value of the cell.
        */
       public char getToken()
       {
           return token;
       }

       /**
        * Sets the token of the cell.
        * @param c Character to use as token value.
        */
       public void setToken(char c)
       {
           token = c;
           repaint();
       }

       @Override
       protected void paintComponent(Graphics g)
       {
           super.paintComponent(g);

           if (token == 'X')
           {
               g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
               g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
           }

           else if (token == 'O')
           {
               g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
           }
       }
       // MouseListener to listen for a click to place a token
       private class MyMouseListener extends MouseAdapter
       {

        private String playAgain;
        private Object playYes;
        private Object playNo;

        @Override
           public void mouseClicked(MouseEvent e)
           {
               if (gameOver)
                   return;

               // if condition not met, the game is not over
               if (token == ' ' && whoseTurn != ' ')
                   setToken(whoseTurn);

               // Check game status
               if (isWon(whoseTurn))
               {
                   jlblStatus.setText("Congratulations " + whoseTurn +  ", You Are Winnner! Game Over! Exit And Restart The Program To Play Again!" );
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else if (isFull())
               {
                   jlblStatus.setText("Tie game! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else
               {
                   whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                   jlblStatus.setText(whoseTurn + "'s turn to play.");

               }
             // and ends here, while statement is below
               {
               // if the game is over, this will run           
               if (gameOver == true)

                   playAgain = JOptionPane.showInputDialog("Would you like to play again?");

               // if statement that either restarts the game or closes it depending on the yes/y (cases are ignored) answer

               while (playAgain.equalsIgnoreCase("y") | playAgain.equalsIgnoreCase("yes")) 
               {
                   playYes = ("Yes? Okay, have fun!");

                   JOptionPane.showMessageDialog( null, playYes );
      }   
     }         
    }// End of Win condition checker
   } // End class MyMouseListener           
  }
} // End class TicTacToeFrame

You could initialize all of your variables from a single method. 您可以通过一个方法初始化所有变量。 This way you could initialize the game at the beginning of the program with it and if the player requests to play the game again you could call the same method thus bringing the program back to its original state. 这样,您可以在程序开始时用它来初始化游戏,如果玩家要求再次玩游戏,您可以调用相同的方法,从而使程序回到其原始状态。

private void init() {
    whoseTurn = 'X';
    gameOver = false;
    jlblStatus = new JLabel("X's turn to play");
    for(Cell[] ca : cells) {
        for(Cell c : ca) {
            c.setToken(' ');
        }
    }
}

My example might not include all variables but you could tweak it to suit your needs. 我的示例可能未包含所有变量,但您可以对其进行调整以满足自己的需求。

The do-while loop is not recognized because it is outside of a method. 无法识别do-while循环,因为它在方法外部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM