Home java Noliki crossbars

Noliki crossbars

Author

Date

Category

Trying to realize the game of the Grub-Noliki for Java. Everything seems to work as thought, but the continuation of the game does not go at all.

The essence is that the exit () method is looped. When I enter “n” everything works well, and when I enter “Y” asks for a new one.

Tell me, please, what is the error.


package tic_tac_toe;
Import java.util.scanner;
Public Class XO {
  Public Static Void Game () {
    Scanner in = New Scanner (System.in); // Connect the read class info and (Alya read)
    String Hole; // variable location
    String [] [] Field = New String [3] [3];
    // Fill the array of spaces, for subsequent replacement of their x or about
    For (int i = 0; i & lt; 3; i ++) {
      System.Out.printLN ();
      For (int j = 0; j & lt; 3; j ++) {
        Field [i] [j] = ("[]");
        System.Out.print (Field [i] [J]);
      }
    }
    int i = 2;
    int quit = 0;
    While ((i & lt; 11) & amp; (quit == 0)) {
      if (I% 2 == 0) {
        System.out.PrintLN ("\ N");
      }
      ELSE {
        System.out.printLN ("\ N");
      }
      System.out.print ("Enter the cell: & gt;");
      // Read input, and move.
        Switch (Hole = in.next ()) {
        Case "7": {
          if (field [0] [0] == ("[]")) {
            if (I% 2 == 0) {
              Field [0] [0] = ("[x]");
              I ++;
              Break;
            }
            ELSE {
              Field [0] [0] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "8": {
          if (field [0] [1] == ("[]")) {
            if (I% 2 == 0) {
              field [0] [1] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [0] [1] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "9": {
          if (field [0] [2] == ("[]")) {
            if (I% 2 == 0) {
              field [0] [2] = ("[x]");
              I ++; Break;
            }
            ELSE {
              Field [0] [2] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "4": {
          if (field [1] [0] == ("[]")) {
            if (I% 2 == 0) {
              field [1] [0] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [1] [0] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "5": {
          if (field [1] [1] == ("[]")) {
            if (I% 2 == 0) {
              field [1] [1] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [1] [1] = ("[o]");
              I ++; Break;
            }
          }
          ELSE { 
System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "6": {
          if (field [1] [2] == ("[]")) {
            if (I% 2 == 0) {
              field [1] [2] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [1] [2] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        CASE "1": {
          if (field [2] [0] == ("[]")) {
            if (I% 2 == 0) {
              field [2] [0] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [2] [0] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        Case "2": {
          if (field [2] [1] == ("[]")) {
            if (I% 2 == 0) {
              field [2] [1] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [2] [1] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        Case "3": {
          if (field [2] [2] == ("[]")) {
            if (I% 2 == 0) {
              Field [2] [2] = ("[x]");
              I ++; Break;
            }
            ELSE {
              field [2] [2] = ("[o]");
              I ++; Break;
            }
          }
          ELSE {
            System.out.PrintLN ("This cell is busy, enter the other."); Break;
          }
        }
        Default:
          System.Out.printLN ("\ N erubring again!");
          Break;
        }
      // Field output on screen
      For (int p = 0; p & lt; 3; p ++) {
        System.Out.printLN ();
        for (int l = 0; l & lt; 3; l ++) {
          System.out.print (field [p] [L]);
        }
      }
      For (int t = 0; T & LT; 3; T ++) {
      // Check direct
        if (((field [T] [0]) == "[o]" & amp; (field [t] [1]) == "[o]" & amp; field [t] [2] == "[ O] ") ||
             ((Field [0] [T]) == "[O]" & amp; (field [1] [t] == ​​"[o]" & amp; field [2] [t] == ​​"[o] ")) {
          System.Out.print ("\ NPB about");
          quit ++; Exit ();
        }
        Else If ((((field [t] [0]) == "[x]" & amp; (field [t] [1]) == "[x]" & amp; field [t] [2] == " [X] ") ||
             ((Field [0] [T]) == "[x]" & amp; (field [1] [t] == ​​"[x]" & amp; field [2] [t] == ​​"[x] ")) {
          System.Out.print ("\ N Pobedle x");
          quit ++; Exit ();
        }
        ELSE {}
      }
      // Check diagonal
      if (((field [0] [0]) == "[x]" & amp; (field [1] [1]) == "[x]" & amp; field [2] [2] == " X] ") ||
           (((Field [0] [2]) == "[x]" & amp; (field [1] [1]) == "[x]" & amp; field [2] [0] == "[x] ")) {
        System.Out.print ("\ N Pobedle x");
        quit ++; Exit ();
      }
      ELSE If (((((0] [0]) == "[o]" & amp; (field [1] [1]) == "[o]" & amp; field [2] [2] == " [O] ") ||
           ((Field [0] [2]) == "[O]" & amp; (field [1] [1]) == "[O]" & amp; field [2] [0] == "[O] ")) {
        System.out.print ( "\ nPobedil O"); 
quit ++; Exit ();
      }
      ELSE {}
    }
      System.Out.printLN ();
    }
  Public Static Void Exit () {// There is no continuation of the game
    Scanner EX = NEW Scanner (System.in);
    String Re;
    do {
      System.Out.print ("Want to continue the game?");
      RE = EX.NEXT ();
      if (Re == "Y") {
        Game ();
      }
      Else if (Re == "n") {
        Break;
      }
    } While (Re == ("Y") || Re == "n");
  }
  Public Static Void Main (String [] Args) {
    Game ();
    // exit ();
  }
}

Answer 1, Authority 100%

Rows are compared by Equals (String Str)
That is, you should have:

do {
  System.Out.print ("Want to continue the game?");
  RE = EX.NEXT ();
  If (Re.equals ("Y")) {
    Game ();
  }
  ELSE If (Re.equals ("N")) {
    Break;
  }
} While (Re.equals ("N") || Re.equals ("Y"));

And when you enter N ", the flow comes out of the cycle only because you used early exit break from the cycle.


Answer 2

package com.company;
Import java.util.scanner;
Public Class Main {
  Public Static Void Main (String [] Args) {
    char [] [] field = new char [3] [3];
    Scanner Scanner = New Scanner (System.in);
    While (True) {
      System.out.print ("Crossings, enter the cell number horizontally, starting at 0:");
      int a = scanner.nextint ();
      System.Out.print ("Crossings, vertically:");
      int b = scanner.nextint ();
      field [a] [b] = 'x';
      For (int i = 0; i & lt; 3; i ++) {
        For (int j = 0; j & lt; 3; j ++) {
          System.out.Print (Field [i] [j] + "|");
        }
        System.Out.printLN ();
      }
      System.out.print ("Noliki, enter the cell number horizontally, starting with 0:");
      int c = scanner.nextint ();
      System.Out.print ("Noliki, vertically:");
      int d = scanner.nextint ();
      Field [C] [D] = '0';
      For (int i = 0; i & lt; 3; i ++) {
        For (int j = 0; j & lt; 3; j ++) {
          System.out.Print (Field [i] [j] + "|");
        }
        System.Out.printLN ();
      }
      If (field [0] [0] == FIELD [0] [1] & amp; & amp; field [0] [1] == field [0] [2] & amp; & amp; field [0] [2] = = 'x' || field [0] [2] == 'y') {
        System.Out.printLN ("Win" + Field [0] [2]);
        Break;
      }
      ELSE If (field [0] [0] == field [1] [0] & amp; & amp; field [1] [0] == field [2] [0] & amp; & amp; field [2] [0] == 'x' || field [2] [0] == 'y') {
        System.out.PrintLN ("Win" + Field [0] [0]);
        Break;
      }
      ELSE If (field [0] [1] == field [1] [1] & amp; & amp; field [1] [1] == field [2] [1] & amp; & amp; field [2] [1] == 'x' || field [2] [1] == 'y') {
        System.out.PrintLN ("Win" + Field [0] [1]);
        Break;
      }
      ELSE If (field [2] [0] == field [2] [1] & amp; & amp; field [2] [1] == field [2] [2] & amp; & amp; field [2] [2] == 'x' || field [2] [2] == 'y') {
        System.out.PrintLN ("Win" + Field [2] [0]);
        Break;
      }
      ELSE if (field [0] [2] == field [1] [2] & amp; & amp; field [1] [2] == field [2] [2] & amp; & amp; field [2] [2] == 'x' || field [2] [2] == 'y') {
        System.Out.printLN ("Win" + Field [0] [2]);
        Break;
      }
      ELSE If (field [0] [0] == field [1] [1] & amp; & amp; field [1] [1] == field [2] [2] & amp; & amp; field [2] [2] == 'x' || field [2] [2] == 'y') {
        System.out.PrintLN ("Win" + Field [0] [0]);
        Break;
      }
      ELSE if (field [1] [0] == field [1] [1] & amp; & amp; field [1] [1] == field [2] [2] & amp; & amp; field [2] [2] == 'x' || field [2] [2] == 'y') {
        System.Out.printLN ("Win" + Field [1] [0]);
        Break;
      } 
ELSE If (field [0] [2] == field [1] [1] & amp; & amp; field [1] [1] == field [2] [0] & amp; & amp; field [0] [2] == 'x' || field [0] [2] == 'y') {
         System.Out.printLN ("Win" + Field [0] [2]);
         Break;
       }
     }
   }
}

In essence, the same code, but I just want to show it, for this is the first more or less serious “project” :), although still raw pretty.


Answer 3

Just add Break After:

if (re == "y") {
   Game ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions