本文介绍了如何将此代码放在循环中,这样我就不必继续打印相同的东西了。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Drjava做一个Tic Tac Toe游戏。我需要这方面的帮助。所以我不会把整个代码放在一个循环中,所以我不必一遍又一遍地打印相同的东西。



Hi, I am making a Tic Tac Toe game in Drjava. I need help with this thing. So i wont to put this entire code in a loop so I Dont have to keep printing the same thing over and over again.

<pre lang="Javascript">import java.util.Scanner;

public class TicTacToeArray {
  
  public static void main(String[] args){
    
    Scanner in = new Scanner(System.in);
    
    int [] A1 = {0,1,2};
    int [] A2 = {3,4,5};
    int [] A3 = {6,7,8};
    int [] A4 = {0,3,6};
    int [] A5 = {1,4,7};
    int [] A6 = {2,5,8};
    int [] A7 = {0,4,8};
    int [] A8 = {2,4,6};
    
    
    System.out.println("Player #1: Please enter a number between 0-8");
    int playerOne = in.nextInt();  
    
    if (playerOne == A1 [0]){
      System.out.println("           |          |          ");
      System.out.println("    X      |    1     |     2    ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |     5    ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     8    ");
      System.out.println("           |          |          ");
      
      
    }else if (playerOne ==A1 [2]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    X     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |     5    ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     8    ");
      System.out.println("           |          |          ");
      
    }else if (playerOne ==A2 [0]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    X      |    4     |     5    ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     8    ");
      System.out.println("           |          |          ");
      
      
    }else if (playerOne ==A2 [1]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    X     |     5    ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     8    ");
      System.out.println("           |          |          ");
      
      
    }else if (playerOne ==A2 [2]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |    X     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     8    ");
      System.out.println("           |          |          ");
      
      
    }else if (playerOne ==A3 [0]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |    5     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    X      |    7     |     8    ");
      System.out.println("           |          |          ");
      
      
    }else if (playerOne ==A3 [1]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |    5     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    X     |     8    ");
      System.out.println("           |          |          "); 
      
    }else if (playerOne ==A3 [2]){
      System.out.println("           |          |          ");
      System.out.println("    0      |    1     |    2     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");    
      System.out.println("           |          |          ");
      System.out.println("    3      |    4     |    5     ");
      System.out.println("           |          |          ");    
      System.out.println("  -------------------------------");      
      System.out.println("           |          |          ");
      System.out.println("    6      |    7     |     X    ");
      System.out.println("           |          |          ");
      
    }else {
      System.out.println("Try Again");
      
      
    }
  }
}

推荐答案

import java.util.Scanner;

public class TTT
{

  public static void draw_tiles(int x)
  {
    int c, r; // r is the row number, c is the column number
    for (r = 0; r < 3; ++r)
    {
      System.out.println("           |           |          ");
      for ( c = 0; c < 3; ++c)
      {
        String sep, no;
        int n = r * 3 + c;
        sep = c < 2 ? "|" : "\n";
        System.out.print("     ");
        no = x == n ? "X" : String.valueOf(n);
        System.out.print(no);
        System.out.print("     ");
        System.out.print(sep);
      }
      System.out.println("           |           |          ");
      if ( r < 2 )  System.out.println("----------------------------------");

    }
  }

  public static void main(String[] args)
  {

    Scanner in = new Scanner(System.in);
    System.out.println("Player #1: Please enter a number between 0-8");
    int playerOne = in.nextInt();
    if ( playerOne >= 0 && playerOne <= 8)
      draw_tiles(playerOne);
    else
      System.out.println("Try Again");
  }
}


这篇关于如何将此代码放在循环中,这样我就不必继续打印相同的东西了。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:33