如何使用在另一种方法中更改的数组?

时间:2016-03-08 01:04:23

标签: java arrays

我在类级别(gameGrid)声明了一个数组,然后我在一个方法中进行了更改,但现在我需要采用这种改变的方法(现在填充".")并做更多另一种方法(printGrid)。我的教授在评论中帮助我处理了包含JRD的所有代码,所以我需要保留这些想法。我有return声明,但这并不能帮助解决问题。这个程序是一个非常简单的PacMan游戏。我使用的是Java 8,但只能使用最基本的概念:变量,基本数据类型,数组,控制语句,字符串方法,只有一个类,并且有多种方法都可以。

import java.util.Scanner;

public class PacManGame
{

static char [][] gameGrid;      // Define here so all methods can use it.  JRD.
static int X, Y;                //Variables for number of grid rows X, and columns Y  

public static void main(String[] args)
{
int numMoves; // How to keep track of # of moves user makes before winning game

Scanner input = new Scanner( System.in );

System.out.println();
System.out.print( "Enter the number of rows you would like in your game grid: " );
X = input.nextInt();
System.out.println();
System.out.print( "Enter the number of columns you would like in your game grid: " );
Y = input.nextInt(); 
System.out.println(); 

buildGrid(X, Y);  // Calls buildGrid method 
playGame();  // Need to start the game which includes printing the menu.  JRD.

} // Closes main method

public static void buildGrid(int X, int Y) // Return the built grid so other methods can use it. JRD. // Method for actually building the grid
{
    gameGrid = new char[X][Y]; // Array built from user's input for dimensions, with char. Defined at class level.   JRD.  
    int totalGridSize = X * Y;               // Gets the total grid size
    int cookieTotal = totalGridSize / 5;     // Calculates the 20% of cookies that will be on grid

    int theCookies = (int)(cookieTotal*Math.random())+1; //Assigns the randomly generated number  

    int i, j, k = 0;                         // Initialize loop counters
    for (i = 0; i < X; i++)
        {
            for (j = 0; j < Y; j++)
                {
                    gameGrid[i][j] = '.';

                }
        }
    return gameGrid[i][j];
 } // Closes buildGrid method

public static void printGrid()
{
    char currentGrid [][] = new gameGrid[i][j];
    currentGrid[0][0] = '<';


}
}

编辑:以下是我的新代码:

import java.util.Scanner;

public class PacManGame
{

    static char [][] gameGrid;      // Define here so all methods can use it.  JRD.
    static int X, Y;                //Variables for number of grid rows X, and columns Y  
    static int theCookies;
    public static void main(String[] args)
    {
    int numMoves; // How to keep track of # of moves user makes before winning game

    Scanner input = new Scanner( System.in );

    System.out.println();
    System.out.print( "Enter the number of rows you would like in your game grid: " );
    X = input.nextInt();
    System.out.println();
    System.out.print( "Enter the number of columns you would like in your game grid: " );
    Y = input.nextInt(); 
    System.out.println(); 

    buildGrid(X, Y);  // Calls buildGrid method 
    playGame();  // Need to start the game which includes printing the menu.  JRD.

    } // Closes main method

    public static char[][] buildGrid(int X, int Y) // Return the built grid so other methods can use it. JRD. // Method for actually building the grid
    {
        gameGrid = new char[X][Y]; // Array built from user's input for dimensions, with char. Defined at class level.   JRD.  
        int totalGridSize = X * Y;               // Gets the total grid size
        int cookieTotal = totalGridSize / 5;     // Calculates the 20% of cookies that will be on grid

        theCookies = (int)(cookieTotal*Math.random())+1; //Assigns the randomly generated number  

        int i, j = 0;                         // Initialize loop counters
        for (i = 0; i < X; i++)
            {
                for (j = 0; j < Y; j++)
                    {
                        gameGrid[i][j] = '.';

                    }
            }
        return gameGrid;
     } // Closes buildGrid method

    public static char[][] printGrid(char[][] gameGrid )
    {
        gameGrid[0][0] = '<';
        int i, j = 0;
        for (i = 0; i < X; i++)
            {
                for (j = 0; j < Y; j++)
                    {
                        gameGrid[theCookies][theCookies] = 'O';  
                    }
            }
        return gameGrid;
    }

    public static void printMenu() 
    {
        System.out.println();
        System.out.println( "Choose from the following menu:");
        System.out.println( "1) Turn left");
        System.out.println( "2) Turn right");
        System.out.println( "3) Move");
        System.out.println( "4) Exit");
    } 

    // public static int playGame(int choice)
    public static void playGame() // Don't need to return anything. Add passed grid.  JRD.
    {
        int numMoves = 0; // Move declaration here.  JRD. // Will be used to keep track of # of moves
        int choice; // This is only used locally and not externally.  JRD.


        do
        {
            printGrid(gameGrid); // Print grid JRD.
            printMenu(); // Print menu for each command.  JRD.
            Scanner input = new Scanner( System.in );
            System.out.print( "Enter your menu choice (1-4): ");
            choice = input.nextInt();

            switch (choice)
            {
            case 1:

                System.out.print(">");
                break;    
            case 2:

                System.out.print("^");
                break;
            case 3:

                System.out.print("V");
                numMoves++;

                break;
            }
        System.out.println( "If you would like to see the menu again, enter 1: " );
        int menuChoice = input.nextInt();
        if (menuChoice == 1)
            {
                printMenu();
                break;
            }
        }
        while (choice != 4);

        System.out.print( "Thanks for playing! You took " + numMoves + " moves to eat all the cookies." );


    } // Closes playGame method

} // Closes PacManGame class

1 个答案:

答案 0 :(得分:0)

让网格创建方法返回网格

public static char[][] buildGrid(int X, int Y) // Return the built grid so other methods can use it. JRD. // Method for actually building the grid
{
    char[][] gameGrid = new char[X][Y]; // Array built from user's input for dimensions, with char. Defined at class level.   JRD.  

    // the same as original code, omitted

    return gameGrid;
} // Closes buildGrid method

并将网格修改方法传递给网格并进行修改

public static void printGrid(char[][] currentGrid)
{
    currentGrid[0][0] = '<';
}

如何使用方法:

char[][] grid = buildGrid(X, Y);
putStartOnGrid(grid);

另一个选择:你使gameGrid成为一个成员属性,所以你可能不想返回它并且让函数只使用成员属性。

public static void buildGrid(int X, int Y) // Return the built grid so other methods can use it. JRD. // Method for actually building the grid
{
    gameGrid = new char[X][Y]; // Array built from user's input for dimensions, with char. Defined at class level.   JRD.  

    // the same as original code, omitted

    // remove the return statement
} // Closes buildGrid method

public static void printGrid()
{
    // you already have built gameGrid, so you don't have to try to create it again here
    gameGrid[0][0] = '<';
}