2014-12-05 94 views
-1

所以我正在處理一個名爲planner的類[它沒有main,因爲它是一個單獨的類],但我在使用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() { 

} 
} 
+0

爲什麼你把if/else語句放在一行上?它使你的代碼難以閱讀 – Sybren 2014-12-05 17:53:28

+0

else {Planner();},這是做什麼的? – 2014-12-05 17:55:00

回答

0

當你聲明

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

應該

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

您不能直接具有可執行代碼的類。你應該在方法或構造函數中擁有它。 否則,如果你想初始化數組,你必須像這樣寫int [] anArray = new int [1000];