Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help add something to my code please.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help add something to my code please.

    This is part of my code for a simple tic tac toe.

    How can I stop the game from running?


     
     
     
    import java.util.Scanner;
     
    /**
       This program runs a TicTacToe game. It prompts the
       user to set positions on the board and prints out the
       result.
    */
    public class TicTacToeRunner
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          String player = "x";
          TicTacToe game = new TicTacToe();
          boolean done = false;
          while (!done)
          {
             System.out.print(game.toString()); 
             System.out.print(
                   "Row for " + player + " (-1 to exit): ");
             int row = in.nextInt();
             if (row < 0) done = true;
             else
             {
                System.out.print("Column for " + player + ": ");
                int column = in.nextInt();
                game.set(row, column, player);
     
     
     
    //add something here to stop?
     
     
     
                if (player.equals("x")) 
                   player = "o"; 
                else 
                   player = "x";    
             }
          }
       }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help add something to my code please.

    You could set done equal to true...
    Or you could use a break statement to short circuit the loop

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help add something to my code please.

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

Similar Threads

  1. How to add strings to this Code?
    By HARMAN in forum Java Theory & Questions
    Replies: 3
    Last Post: June 8th, 2013, 08:00 PM
  2. How to add buttons on this code
    By tarell85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 18th, 2011, 07:08 AM
  3. Add code to an existing project
    By atul.mathur31 in forum Java IDEs
    Replies: 1
    Last Post: January 5th, 2011, 06:50 PM
  4. need to add a Double Button to the code
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 5th, 2010, 11:19 AM
  5. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM