简体   繁体   English

PacMan网格2D阵列JAVA

[英]PacMan Grid 2D Array JAVA

I'm supposed to make a simple type text PacMan game in Java and for the board I have to use a 2D array to make the grid. 我应该用Java制作一个简单的文本PacMan游戏,对于板子,我必须使用2D数组制作网格。 Here are the exact directions: At program startup, constructs and displays a 2-dimensional grid using standard array(s) (no collection classes allowed) with the size dynamically specified by the user (X and Y sizes can be different). 确切的方向如下:在程序启动时,使用标准数组(不允许集合类)构造和显示二维网格,并由用户动态指定大小(X和Y的大小可以不同)。 Places the PacMan in the upper-left corner of the grid facing left All grid cells should have the empty cell character of '.' 将PacMan放置在网格的左上角(面向左侧)。所有网格单元的空单元格字符应为“。”。

This is my code so far, but I keep getting errors and I'm not sure how to fix it: 到目前为止,这是我的代码,但是我不断收到错误,并且不确定如何解决它:

public class myPacMan {
public static void main(String[] args){
    Scanner input = new Scanner (System.in);
    System.out.print("Choose an x value:");
    int x = input.nextInt();
    System.out.print("Choose a y value:");
    int y = input.nextInt();
    int grid [][] = new int [x][y];
    int i, j = 0;
    for(i=0; i<x; i++);
    for(j=0; j<y; j++);
    System.out.print(grid[x][y] + ".");
}

} }

Two things. 两件事情。 First, remove the semicolons after your for loops. 首先,在for循环后删除分号。 Second, your print statement should use i and j, instead of x and y. 其次,您的打印语句应使用i和j,而不是x和y。 X and Y will always be one more than your array, so you'll get an index out of bounds exception. X和Y总是比数组大1,因此您将获得索引超出范围的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM