【问题标题】:How to return an two dimensional array index from a method?如何从方法返回二维数组索引?
【发布时间】:2015-01-29 22:35:05
【问题描述】:

我正在从这里创建一个井字游戏:

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

我在找出如何从方法返回数组索引时遇到了麻烦。我希望用户输入 2 个整数(行、列)并返回该索引并让字符“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;
            }

    }

}

【问题讨论】:

  • 您是在谈论您的 getArrayIndex() 方法吗?如果是这样,现在的方式有什么问题?
  • 毫无疑问,您可能会发现使用具有 9 个元素的标准数组编写井字游戏比使用二维数组更容易。
  • 嗨贾斯汀。你说过你想从一个方法返回一个数组索引。数组索引只是一个 int,从给定索引的函数返回索引,只是将输入返回给方法。您的描述可能令人困惑。数组具有索引和值 多维数组具有也是数组的值。 int[][] foo = {{0, 1, 2}, {3, 1, 4}, {1, 1, 2}} 是一个包含 9 个条目的二维数组。访问二维数组中的值时,即foo[1][2],您首先引用的是 foo[] 的索引 1 处的数组,然后是 foo[1] 的索引 2 处的 int,即 4

标签: java arrays methods


【解决方案1】:

您可能需要考虑使用 java.awt.Point。

【讨论】:

    【解决方案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};
        }
    

    您返回了一个包含所需索引的双元素数组。这是您可以完成它的一种方法。其他形式包括使用两个 int 元素创建一个 Object 并使用它,或者以某种方式将两个变量存储在同一个 int 中(例如,保持一位数长)。 无论如何,您返回这些索引的方式没问题。您如何使用这些信息取决于您自己。我猜你必须将它存储在一个临时的 int 数组中,比如int[] temp=getArrayIndex();,然后访问每个元素以获取相应的索引。

    但是,您的代码还有其他错误,例如忘记为该函数中的参数命名(或者实际上没有使用此类参数):

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

    我建议你检查一下。也许你编译的程序给了你一个错误,你认为这是因为new int[] {r,c} 的事情,但是不,那行得通。如果是这种情况,请检查其余部分,这是另一回事。不过,我很乐意提供帮助,所以请随时提问。 ^^

    【讨论】:

      【解决方案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 人员更喜欢这种对类型的明确说明,而不是使用整数数组。

      【讨论】:

      • 总的来说我同意,但是这个孤立的课程对于像 OP 这样的初学者来说并没有多大作用。
      • @Robert 我必须承认,在没有真正理解 OP“遇到问题”的情况下,我有点仓促地发布了这个答案。所以我不确定要在我的答案中添加什么以使其更有用。 OP 提供的更多信息会有所帮助。
      猜你喜欢
      • 2021-05-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多