基本Java-无法从main中的单独方法显示数组

时间:2014-11-12 03:47:02

标签: java arrays methods main

*编辑 - 我修复了代码,我完全偏离了原始代码。这是我现在的程序。它的目的是类似于连接四个游戏,而不是通过对角线,水平或垂直连接获胜。获胜的唯一方法是垂直连接四个X.我现在无法搞清楚可能获胜的代码。有什么想法吗?

package stackemupnew;

import java.util.Scanner;

公共类StackEmUpNew {

public static void main(String[] args) {

    //declares the game space array
    char[][] gameSpace = new char[6][6];
    int[] rowTrack = new int[]{5, 5, 5, 5, 5, 5};

    //prints number of columns above the board
    for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");

    //sets the game space to it's starting state
    for (int i = 0; i < gameSpace.length; i++) {
        for (int j = 0; j < gameSpace[i].length; j++) {
            gameSpace[i][j] = '.';
        }
    }

    for (int i = 0; i < gameSpace.length; i++) {
        for (int j = 0; j < gameSpace[i].length; j++) {
            System.out.print(gameSpace[i][j] + " ");
        }
        System.out.println("");
    }
    //prints number of columns under the board
    for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");



    while (winDetector(gameSpace) == false) {

        //run the method that lets the user make a move
        userMove(gameSpace, rowTrack);
        for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");

        for (int i = 0; i < gameSpace.length; i++) {
            for (int j = 0; j < gameSpace[i].length; j++) {
                System.out.print(gameSpace[i][j] + " ");
            }
            System.out.println("");
        }

     for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");

    //run the method that checks for a win
    winDetector(gameSpace);

        //run the method that makes a computer move
        computerMove(gameSpace, rowTrack);

        System.out.println("The computer's move is:");

        for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");


        for (int i = 0; i < gameSpace.length; i++) {
            for (int j = 0; j < gameSpace[i].length; j++) {
                System.out.print(gameSpace[i][j] + " ");
            }
            System.out.println("");
        }

        for (int c = 0; c < 6; c++) {
        System.out.print(c + " ");

    }
    System.out.println(" ");
    }
    winDetector(gameSpace);
}

//detects a win NOT FINISHED
public static boolean winDetector(char[][] gameSpace){ 
 boolean win = false;   
    for (int i = 0; i < gameSpace.length; i++) {
            for (int j = 0; j < gameSpace[i].length; j++){
                if (gameSpace[i][j]=='X'&&
                    gameSpace[i][j+1]=='X'&&
                    gameSpace[i][j+2]=='X'&&
                    gameSpace[i][j+3]=='X'){
                    System.out.println("Congratulations! You win!");
                    win = true;
                }
            }
    }
return win;
}

public static void userMove(char[][] gameSpace, int[] rowTrack) {

    Scanner input = new Scanner(System.in);

    System.out.print("Where would you like to drop an X?");

    int col = input.nextInt();
    int row = rowTrack[col];

    rowTrack[col]--;

    gameSpace[row][col] = 'X';
}

public static void computerMove(char[][] gameSpace, int[] rowTrack) {

    int col = (int) (Math.random() * 6);
    int row = rowTrack[col];

    rowTrack[col]--;

    gameSpace[row][col] = 'O';
}

}

 The output reads:
 0 1 2 3 4 5  
 . . . . . . 
 . . . . . . 
 . . . . . . 
 . . . . . . 
 . . . . . . 
 . . . . . . 
 0 1 2 3 4 5  
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at stackemupnew.StackEmUpNew.winDetector(StackEmUpNew.java:107)
    at stackemupnew.StackEmUpNew.main(StackEmUpNew.java:48)
 Java Result: 1
 BUILD SUCCESSFUL (total time: 0 seconds)

4 个答案:

答案 0 :(得分:0)

首先,如果您打算拨打gameSpaceInitial(),则需要添加括号。

System.out.println(gameSpaceInitial());

其次,定义为char [] []的变量是char数组的数组。所以对于以下代码:

gameSpace = new char[6][6];

...... gameSpace.length的值应为6。

第三,您的gameSpaceInitial方法需要返回gameSpaceInitial,但代码中没有额外的'}'。

第四,如果您将char[][]传递给System.out.println(),您将看到char[][]的内存引用,而不是内容。

答案 1 :(得分:0)

方法gameSpaceInitial(int i,int j)接受两个int类型参数。你忘了通过它们了。

你需要做类似的事情,

System.out.println(gameSpaceInitial(2,3));

答案 2 :(得分:0)

这是因为您只打印gameSpace的长度。这不包括数组值中的元素数。

public class StackEmUp {

    public static void main ( String[] args ) {
        char[][] gameSpace = gameSpaceInitial( 6, 6 );

        int rows = gameSpace.length;
        System.out.println( "Rows: " +  rows );

        int elements = 0;
        for ( int i = 0; i < rows; i++ ) {
            elements += gameSpace[i].length;
        }

        System.out.println( "Elements: " + elements );
        ...
    }

    private static char[][] gameSpaceInitial ( int rows, int cols ) {
        char[][] gameSpaceInitial = new char[rows][cols];

        for ( int i = 0; i < rows; i++ ) {
            for ( int j = 0; j < cols; j++ ) {
                gameSpaceInitial[i][j] = '.';
            }
        }

        return gameSpaceInitial;
    }

}

答案 3 :(得分:0)

这里的答案都不完全正确。每个人都在触摸需要修复的代码部分,但需要将它们组合在一起,另外还要修复一些其他内容。

请查看此小提示以进行更正以使其正常工作: http://ideone.com/ga62vf

public static void main (String[] args)
{
    int i=6;
    int j=6;

    //initialize the array
    char[][] gameSpace; 
    gameSpace = gameSpaceInitial(i, j);

    //display the initial game space
    DisplayGameSpace(gameSpace);
}

//the intial game board without any pieces
static char[][] gameSpaceInitial(int maxRows,int maxColumns){

    char[][] emptyBoard = new char[maxRows][maxColumns];

    for (int row = 0; row < maxRows; row++) {
        for (int col = 0; col < maxColumns; col++) {
            emptyBoard[row][col] = '.';
        }
    }

    return emptyBoard;   
}

所以要解释一下代码中的修复内容:

char[][] gameSpace; 
gameSpace = new char[i][j];

这只会创建一个空的2D数组。然后,您需要使用所需的值(.)填充它。您要做的是调用gameSpaceInitial方法将空游戏板分配给gameSpace。 当你调用gameSpaceInitial时,你已经定义了两个需要传递给它的参数,否则你的代码将无法编译。在您的示例中,它需要两个int s ij。我重命名它们以赋予它们更多意义并使代码可读。然后你要做的是遍历每一行,然后每一列用所需的字符填充它。然后我们返回这个新的char数组,它将被分配给gameSpace

然后为了更容易地显示电路板,我添加了一个额外的方法,通过行和列执行相同的双循环来打印每个字符:

static void DisplayGameSpace(char[][] gameSpace) {
    for (int row = 0; row < gameSpace.length; row++) {
        for (int col = 0; col < gameSpace[row].length; col++) {
            System.out.print(gameSpace[row][col]);
        }
        System.out.println();
    }
}