如何从方法返回二维数组索引? [英] How to return an two dimensional array index from a method?

查看:84
本文介绍了如何从方法返回二维数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里创建井字游戏程序

Im creating a tic tac toe program from here:

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

我在找出如何从方法返回数组索引时遇到麻烦.我想让用户输入2个整数(行,列)并返回该索引,并用char'O'或'X'替换空白的对应索引.

I'm having trouble finding out how to return an array index from a method. I want to user to input 2 integers, ( row, column) and have that index returned and have either char 'O', or 'X' replace the blank corresponding index.

我对Java有点陌生,并试图尽可能快而有效地学习它,以适应我的课堂.

I'm a bit new to java, and am trying to learn it as fast and efficiently as possible to catch up in my class.

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;
            }

    }

}

推荐答案

执行此操作的Java方法是定义一个用于保存行和列索引的类.

The Java-way to do this is define a class to hold the row and column index.

final class BoardIndex {

    final int row;
    final int col;

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

然后您可以从函数中将其返回.

You can then return it from a function.

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

键入很多,但是许多Java人士更喜欢这种对类型的显式表示,而不是使用整数数组.

It's quite a bit of typing but many Java folks prefer this kind of being explicit about types over using, say, an array of integers.

这篇关于如何从方法返回二维数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆