数组的奇怪问题
问题描述:
所以我正在处理一个名为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() {
}
}
答
当你声明
int[] anArray;
anArray = new int [1000];
应该
int[] anArray = new int [1000];
答
您不能直接具有可执行代码的类。你应该在方法或构造函数中拥有它。 否则,如果你想初始化数组,你必须像这样写int [] anArray = new int [1000];
为什么你把if/else语句放在一行上?它使你的代码难以阅读 – Sybren 2014-12-05 17:53:28
else {Planner();},这是做什么的? – 2014-12-05 17:55:00