'.class'预期错误

时间:2013-02-27 03:54:01

标签: java arrays class object loops

您好我正在进行碰碰车游戏,我收到了错误

P:\162>javac bumpercardemo.java

bumpercardemo.java:13:错误:'。class'预期                 printGrid(bcar [],NUMCARS);                                 ^ bumpercardemo.java:34:错误:预期         public static void printGrid(bcar [],int NUMCARS)                                            ^ 2个错误

我是新程序员,从未见过这种类型的错误。

import java.util.Random;

public class bumpercardemo
{
public static void main (String args[])
{
    final int NUMCARS = 16;
    bumpercar bcar[] = new bumpercar[NUMCARS];
    createcars(NUMCARS, bcar);
    printGrid(bcar[], NUMCARS);
}

public static void createcars(int NUMCARS, bumpercar bcar[])
{
    int x=0;
    int y=0;
    Random generator = new Random();
    char symbol = 'A';

    for (int i=0; i<NUMCARS; i++)
    {
        bcar[i] = new bumpercar(x, y, symbol);
        symbol++;

        x = generator.nextInt(bcar[0].getCarSymbol());
        y = generator.nextInt(bcar[0].getCarSymbol());
    }
}


public static void printGrid(bumpercar bcar[], int NUMCARS)
{
    //nested loop
    for(int j = 0; j < 16; j++)
    {
    System.out.printf("\n");
        for(int k = 0; k<16; k++)
        {
            for(int l = 0; l<NUMCARS; l++)
            {
            if((bcar[l].getX() == k) && bcar[l].getY() == j)
            System.out.printf("%s", bcar[l].getCarSymbol());
            else
            System.out.printf("- ");
            }
        }
    }
}


}

我认为它与[]有关,但由于我传入一个数组,我不知道还能做什么。

我检查了Why am I getting a '.class' expected error? Simple Array script,但我无法完全理解它与我的程序有什么关系。

感谢

1 个答案:

答案 0 :(得分:3)

取出[]。由于您要关闭数组,因此只需传递变量名称。

printGrid(bcar, NUMCARS);

你在调用createcars()时做得很对,所以只是模仿那种风格。