2014-12-01 79 views
1

我可是从这里创建一个井字程序:如何从一个方法返回一个二维数组索引?

http://programmingbydoing.com/a/tic-tac-toe.html

我有麻烦找出如何从一个方法返回一个数组索引。我想要用户输入2个整数(行,列),并返回索引,并有char'O'或'X'替换空白对应的索引。

我对Java有点新,并且正努力学习它以尽可能快速和高效地赶上我的课程。

import java.util.Scanner; 

public class ticTacToe{ 
    public static Scanner in = new Scanner(System.in); 
    private static int r, c; 
    private static char[][] board = new char[3][3]; 
    //create a 3x3 array of characters 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 

     displayBoard(); 
     do{ 
      // user inputs two numbers as indexes 
      //turn index into "o" 
      //prompt other user to make move, repeat with "x" 

     }while(checkLoser()); 

    } 

    public static void displayBoard() { 
     System.out.println(" 0 " + board[0][0] + "|" + board[0][1] + "|" + board[0][2]); 
     System.out.println(" --+-+--"); 
     System.out.println(" 1 " + board[1][0] + "|" + board[1][1] + "|" + board[1][2]); 
     System.out.println(" --+-+--"); 
     System.out.println(" 2 " + board[2][0] + "|" + board[2][1] + "|" + board[2][2]); 
     System.out.println("  0 1 2 "); 
    } 

    public static int[] getArrayIndex(){ 
     System.out.println("'O'. Choose your Row...(row, column)"); 
     int r = in.nextInt(); 
     int c = in.nextInt(); 
     return new int[] {r,c}; 
    } 

    public static void userMove(int[][]){ 
     board[r][c] = 'O'; 
    } 

    public static boolean checkLoser(){ 
      if (board[0][0] == board[0][1]){ 
      if (board [0][1] == board[0][2]){ 
       return false; 
      } 
      } 
      else if (board[1][0] == board[1][1]){ 
      if (board [1][1] == board[1][2]){ 
       return false; 
      } 
      } 
      else if (board[2][0] == board[2][1]){ 
      if (board [2][1] == board[2][2]){ 
       return false; 
      } 
      } 
      else if (board[0][0] == board[1][0]){ 
      if (board [1][0] == board[2][0]){ 
       return false; 
      } 
      } 
      else if (board[0][1] == board[1][1]){ 
      if (board [1][1] == board[1][2]){ 
       return false; 
      } 
      } 
      else if (board[0][2] == board[1][2]){ 
      if (board [1][2] == board[2][2]){ 
       return false; 
      } 
      } 

      else if (board[0][0] == board[1][1]){ 
      if (board [1][1] == board[2][2]){ 
       return false; 
      } 
      } 
      else if (board[0][2] == board[1][1]){ 
      if (board [1][1] == board[2][0]){ 
       return true; 
      } 
      }else{ 
       return false; 
      } 

    } 

} 
+3

你说的是你的getArrayIndex()方法吗?如果是这样,目前的方式有什么问题? – 2014-12-01 20:22:42

+1

没有什么,你可能会发现使用9个元素的标准数组编程TicTacToe游戏比使用2维数组更容易。 – 2014-12-01 20:23:15

+0

嗨,贾斯汀。你已经说过你想从_方法返回一个数组索引_。数组索引只是一个int,从给出索引的函数返回索引,只是将输入返回给方法。您的描述可能会感到困惑。 数组有索引和值 多维数组的值也是数组。 int [] [] foo = {{0,1,2},{3,1,4},{1,1,2}}是一个包含9个条目的2D数组。当访问二维数组中的值时,即首先引用的是'foo [1] [2]',在foo []的索引1处的数组,然后是在'foo [1]'的索引2处的int,它是4 – OYRM 2014-12-01 20:33:38

回答

-3

在你的方法:

public static int[] getArrayIndex(){ 
     System.out.println("'O'. Choose your Row...(row, column)"); 
     int r = in.nextInt(); 
     int c = in.nextInt(); 
     return new int[] {r,c}; 
    } 

我相信这将是更安全的事:

public static int[] getArrayIndex(){ 
     System.out.println("'O'. Choose your Row...(row, column)"); 
     int[] retArray = new int[2]; 
     retArray[0] = in.nextInt(); 
     retArray[1] = in.nextInt(); 
     return retArray; 
    } 
+5

这将如何更安全? – 2014-12-01 20:25:48

3

的Java的方式做,这是定义一个类来保存行列索引。

final class BoardIndex { 

    final int row; 
    final int col; 

    BoradIndex(final int r, final int c) { 
     this.row = r; 
     this.col = c; 
    } 
} 

然后,您可以从函数中返回它。

BoardIndex getIt() { 
    return new BoradIndex(1, 2); 
} 

这是相当多的打字,但许多Java乡亲喜欢这种过度使用,也就是说,整数数组是明确的关于类型。

+0

一般来说,我同意,但这个类孤立对于像OP这样的初学者来说不会有太大的帮助。 – Robert 2014-12-01 20:29:21

+0

@Robert我必须承认,我已经有些匆忙发布这个答案,却没有真正理解OP“遇到什么问题”。所以我不确定添加到我的答案,使其更有用。 OP的更多信息将有所帮助。 – 5gon12eder 2014-12-01 20:32:50

1

那么,要回答你的问题,因为它是一个二维数组,你显然需要返回两个索引(正如你可能认为的那样)。由于函数只能返回一个对象,你correcty的意思做给你的函数:

public static int[] getArrayIndex(){ 
     System.out.println("'O'. Choose your Row...(row, column)"); 
     int r = in.nextInt(); 
     int c = in.nextInt(); 
     return new int[] {r,c}; 
    } 

你返回既需要索引包含两个元素的数组。这是你可以完成的一种方式。其他形式包括用两个int元素创建一个对象并使用它,或者以某种方式将这两个变量存储在同一个int中(例如保持它们为一位数字)。无论如何,你的方式来返回这些指标是没问题的。您如何使用这些信息取决于您。我想你必须将它存储在像这样的int[] temp=getArrayIndex();这样的临时int数组中,然后访问每个元素以获取相应的索引。

然而,你的代码有其他错误,就像忘记把一个名称参数在此函数(或者,这样的参数不反正使用的事实):

public static void userMove(int[][]){ 
    board[r][c] = 'O'; 
} 

我建议你检查那些。也许你编译的程序给了你一个错误,你认为这是因为new int[] {r,c}的事情,但naaah,这是有效的。如果是这样,请检查其余部分,这是别的。尽管我很乐意提供帮助,所以请随时提问。 ^^

相关问题