0
public class TicTacToe{
    public static void main(String[] args){
        System.out.println("X starts the game");
        Scanner sc = new Scanner(System.in);
        char a[][] = new char[3][3];
        int n=0;
        int i = 0;
        int j = 0;
        while(n<9){
                System.out.println("Enter the coordinates but not" +i +" " +j);
                int x = sc.nextInt();
                int y = sc.nextInt();
                System.out.println("Enter X");
                a[x][y]=sc.next().charAt(0);
                n++;
                if(n>3){
                    if(a[0][0]=='X' && a[0][1]== 'X' && a[0][2]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[0][0]=='X' && a[1][0]== 'X' && a[2][0]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[1][0]=='X' && a[1][1]== 'X' && a[1][2]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[0][1]=='X' && a[1][1]== 'X' && a[2][1]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[2][0]=='X' && a[2][1]== 'X' && a[2][2]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[0][2]=='X' && a[1][2]== 'X' && a[2][2]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[0][0]=='X' && a[1][1]== 'X' && a[2][2]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                    else if(a[0][2]=='X' && a[1][1]== 'X' && a[2][0]=='X'){
                        System.out.println("Congrats! X won");
                        exit(0);
                    }
                }
                System.out.println("Enter the coordinates but not" +x +" " +y);
                i = sc.nextInt();
                j = sc.nextInt();
                System.out.println("Enter O");
                a[i][j]=sc.next().charAt(0);
                n++;
                if(n>3){
                    if(a[0][0]=='O' && a[0][1]== 'O' && a[0][2]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[0][0]=='O' && a[1][0]== 'O' && a[2][0]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[1][0]=='O' && a[1][1]== 'O' && a[1][2]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[0][1]=='O' && a[1][1]== 'O' && a[2][1]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[2][0]=='O' && a[2][1]== 'O' && a[2][2]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[0][2]=='O' && a[1][2]== 'O' && a[2][2]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[0][0]=='O' && a[1][1]== 'O' && a[2][2]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                    else if(a[0][2]=='O' && a[1][1]== 'O' && a[2][0]=='O'){
                        System.out.println("Congrats! O won");
                        exit(0);
                    }
                }
        }
        sc.close();
    }

    private static void exit(int i) {
        // TODO Auto-generated method stub

    }
}

I have written a code that implements Tic Tac Toe (with baby-steps). The exit(0) is not actually terminating the program execution once X or O wins the game. The program is still asking for the next input. How to terminate the code at that instant ?

2
  • 3
    System.exit(0); Commented Oct 10, 2016 at 6:15
  • 4
    you haven't implemented your exit() method. Commented Oct 10, 2016 at 6:17

1 Answer 1

6

Your exit(0) is calling an empty method. Replace it by this:

private static void exit(int i) {
    System.exit(i);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked now. I'll accept your answer in 8 minutes. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.