4 回答
您的 main 方法不知道是什么events
,因为范围内没有名为 events 的局部变量,主类上也没有名为 events 的静态变量。您需要告诉它您正在查找特定日历对象中的事件对象,如下所示:
System.out.println(calendarObject.events[0]);
这假设您的两个类在同一个包中,或者两者都没有声明包(因此它们都在默认包中)。如果这两个类位于不同的包中,则您必须将实例成员公开为公共或创建一个公共方法来访问它以供另一个查看它。
基于类的对象的想法是您可以使用一个类来创建多个对象。由于您只创建了日历类的一个对象,因此在此作业中可能不会很好地理解这一点。要引用对象的成员,您需要指定要在哪个对象中查找该成员。
首先,它说不要硬编码大小,使用常量。所以像这样制作一个常量:
private static final int ARRAY_SIZE = 5;
并将您的声明更改为
String[] events = new String[ARRAY_SIZE];
并且在初始化数组时,使用 for-counter 循环将每个插槽设置为“-未计划事件-”,而不仅仅是 {一些“-未计划事件”},因为这不是动态的,并且不会随着您的恒定大小而改变。
此外,在您的构造函数中删除类型声明
String[] events
->events
通过声明类型,您创建了一个新的局部变量,而不是修改字段,因此数组中的插槽仍然为空,因为您从未修改它们,而是修改了此局部变量中的字段。
您events
在构造函数中声明了一个本地数组,因此默认值No event planned
没有分配给您的实例变量events
,它应该是
public calender() {
//Initializes your events array
events = { "-No event planned-",
"-No event planned-",
"-No event planned-",
"-No event planned-",
"-No event planned-"};
//end calender class constructor
}
希望它有效
import java.util.Scanner;
//begin class driver
public class driver {
/**
* @paramargs the command line arguments
*/
//begin main method
public static void main(String[] args) {
//initiates scanner
Scanner userInput = new Scanner (System.in);
//declare variables
int dayOfWeek;
String userEvent;
//creates object for calender class
calender calenderObject = new calender();
calender.fillArray();
//user prompt
System.out.println("Enter day of week for your event in the following format:");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
//collect user input
dayOfWeek = userInput.nextInt();
//user prompt
System.out.println("Please type in the name of your event");
//collect user input
userEvent = userInput.next();
//begin while loop
while (dayOfWeek != -1) {
if ((dayOfWeek>=1) && (dayOfWeek<=5)){
calenderObject.createEvent(dayOfWeek, userEvent,dayOfWeek);
for (int i=0;i<calender.length;i++){
System.out.println(calender.events[i]);
}
dayOfWeek = userInput.nextInt();
if (dayOfWeek == -1){
for (int i=0;i<calender.length;i++){
System.out.println(calender.events[i]);
}
System.exit(0);
}
System.out.println("Please type in the name of your event");
userEvent = userInput.next();
} else{
System.out.println("You have entered an invalid number");
System.out.println("Press -1 to quit or enter another day");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
dayOfWeek = userInput.nextInt();
}
}
//end main method
}
}
//begin calender class
public class calender {
static int length = 5;
static String[] events = new String[length];
public static String[] fillArray(){
for (int i=0; i< length;i++){
events[i] = "-No event planned-";
}
return events;
}
//begin createEvent method
public String[] createEvent(int l, String userEvent, int dayOfWeek){
int t = l;
if(t%5 == 1 ){
events[0] = ("Monday Appoinment: ") + userEvent;
} else if(t%5 == 2 ){
events[1] = ("Tuesday Appoinment: ") + userEvent;
} else if(t%5 == 3 ){
events[2] = ("WednsdayAppoinment: ") + userEvent;
} else if(t%5 == 4){
events[3] = ("Thursday Appoinment: ") + userEvent;
} else if(t%5 == 0 ){
events[4] = ("Friday Appoinment: ") + userEvent;
}
return events;
}
//end calender class
}