Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用在其他方法中更改的数组?_Java_Arrays - Fatal编程技术网

Java 如何使用在其他方法中更改的数组?

Java 如何使用在其他方法中更改的数组?,java,arrays,Java,Arrays,我在类级别(gameGrid)声明了一个数组,然后在一个方法中对其进行了更改,但现在我需要使用更改后的方法(现在填充了“),并在另一个方法(printGrid)中对其进行更多操作。我的教授帮助我完成了注释中包含JRD的所有代码,所以我需要保留这些想法。我有一个return语句,但这无助于解决问题。这个程序是一个非常简单的吃豆人游戏。我使用的是Java8,但只能使用最基本的概念:变量、基本数据类型、数组、控制语句、字符串方法、一个类和多个方法都可以 import java.util.Scanner

我在类级别(
gameGrid
)声明了一个数组,然后在一个方法中对其进行了更改,但现在我需要使用更改后的方法(现在填充了
),并在另一个方法(
printGrid
)中对其进行更多操作。我的教授帮助我完成了注释中包含JRD的所有代码,所以我需要保留这些想法。我有一个
return
语句,但这无助于解决问题。这个程序是一个非常简单的吃豆人游戏。我使用的是Java8,但只能使用最基本的概念:变量、基本数据类型、数组、控制语句、字符串方法、一个类和多个方法都可以

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;
公共类PacManGame
{
static char[][]gameGrid;//在此处定义,以便所有方法都可以使用它。JRD。
静态int X,Y;//网格行数X和列数Y的变量
公共静态void main(字符串[]args)
{
int numMoves;//如何跟踪用户在赢得游戏之前的#个动作
扫描仪输入=新扫描仪(System.in);
System.out.println();
System.out.print(“输入您希望在游戏网格中显示的行数:”);
X=input.nextInt();
System.out.println();
System.out.print(“输入您希望在游戏网格中显示的列数:”);
Y=输入。nextInt();
System.out.println();
buildGrid(X,Y);//调用buildGrid方法
playGame();//需要启动游戏,其中包括打印菜单.JRD。
}//关闭主方法
publicstaticvoidbuildgrid(intx,inty)//返回生成的网格,以便其他方法可以使用它。JRD//实际生成网格的方法
{
gameGrid=new char[X][Y];//根据用户输入的维度构建的数组,其中char.Defined在class级别.JRD。
int totalGridSize=X*Y;//获取总网格大小
int cookieTotal=totalGridSize/5;//计算网格上20%的cookie
int theCookies=(int)(cookieTotal*Math.random())+1;//分配随机生成的数字
int i,j,k=0;//初始化循环计数器
对于(i=0;i
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] = '<';
}

另一个选择:您已将
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] = '<';
}
publicstaticvoidbuildgrid(intx,inty)//返回生成的网格,以便其他方法可以使用它。JRD//实际生成网格的方法
{
gameGrid=new char[X][Y];//根据用户输入的维度构建的数组,其中char.Defined在class级别.JRD。
//与原代码相同,省略
//删除return语句
}//关闭buildGrid方法
公共静态void printGrid()
{
//您已经构建了gameGrid,因此不必在此处再次尝试创建它

gameGrid[0][0]='在
buildGrid
上的注释是“返回构建的网格”,那么为什么不返回整个网格,而不是网格中的某个元素呢?(实际上它超出了范围,无法返回任何内容,因为它的返回类型是
void
)我已经将它改为char而不是void,但它仍然无法编译…新游戏网格[I][j]右侧的所有三个部分都给出了错误。我不想只返回一个元素,我希望整个X*Y网格填充“.”,然后将cookies和PacMan分层“好的,我做了您建议的更改,修复了前面描述的错误,但现在另一个方法(我没有在上面粘贴)受到影响。putStartOnGrid必须传递到playGame,所以我将其更改为相同的字符[][]正如您为buildGrid指定的返回类型。我为currentGrid添加了一个返回语句,但它说找不到符号。代码行是putStartOnGrid(currentGrid);它应该不同吗?如果它不编译,它应该不同。lol,也许措辞更好的问题是,“我应该如何更改它?”定义您需要的内容,将被调用方需要的内容作为参数传递,或使用定义的内容。请发布一篇文章,让我们做进一步的检查。注意:请不要打断问题。如果您想发布新代码,请在问题后附加新代码,而不是覆盖现有代码。
char[][] grid = buildGrid(X, Y);
putStartOnGrid(grid);
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] = '<';
}