阵列的奇怪问题

时间:2014-12-05 17:46:14

标签: java eclipse

所以我正在处理一个名为planner的类[没有主要因为它是一个单独的类],但是我在使用eclipse声明一个数组时遇到了问题。您将需要在eclipse中输入它以查看它是什么,因为我不知道它是什么意思'令牌上的语法错误';“,,预期'。

以下是代码:

import java.util.Scanner;

public class Planner {
private int maxEvents = 1000;
private int numEvents = 0;
private int choice;
int[] anArray;
anArray = new int [1000];

public void Planner() {
    Scanner scan = new Scanner (System.in);
    System.out.print("Press 1 to add an event. 2 to display events for the day. 3 to display events for the week. 4 to quit:    ");
    choice = scan.nextInt();
    if (choice == 1){addEvent();}
    if (choice == 2){displayOneDate();}
    if (choice == 3){displayOneWeek();}
    if (choice == 4){System.out.println("Have a good day.");}
    else {Planner();}   
}

public void addEvent() {
    Event newEvent = new Event();
    if (numEvents == maxEvents){System.out.println("Error: No more room.");}
    else {
        for (int i=0; anArray.length > i; i++) {
            numEvents++;
            newEvent.getEventFromUser();
        }
    }
}

public void displayOneDate() {
    System.out.println("Event one: " + anArray[0]);
}

public void displayOneWeek() {

}
}

2 个答案:

答案 0 :(得分:0)

宣布

int[] anArray;
anArray = new int [1000];

应该是

int[] anArray = new int [1000];

答案 1 :(得分:0)

您不能直接在课堂上拥有可执行代码。你应该在方法或构造函数中拥有它。 否则,如果你想初始化数组,你必须写这样的int [] anArray = new int [1000];