为什么我的主类不能在我的日历类中看到数组

时间:2012-10-06 14:38:52

标签: java

这是一个家庭作业问题。我已经迟到了5天,无法弄清楚我做错了什么..这是我在Java的第一个学期,也是我在这个网站上的第一篇文章

这是作业..

创建一个名为Calendar的类。该类应包含一个名为events的变量,它是一个String数组。应该创建数组以容纳5个元素。使用常量值指定数组大小。不要硬编码数组大小。在类构造函数中初始化数组,以便每个元素包含字符串“ - 无事件计划 - ”

该类应包含一个名为CreateEvent的方法。此方法应接受包含单字用户事件的String参数和表示星期几的整数参数。 Monday应使用数字1来表示,Friday应使用数字5来表示。使用传递给方法的事件信息填充events数组。虽然用户将输入单字事件,但每个事件字符串应在每个事件前面添加以下字符串:

event_dayAppoinment: (where event_day is the day of the week)

例如,如果用户输入1和“doctor”,则第一个数组元素应为: 星期一约会:医生

如果用户输入2和“PTA”,则第二个数组元素应为: 星期二任命:PTA

编写一个创建并调用Calendar类的驱动程序(在单独的类中)。然后使用循环来收集用户输入。询问当天(作为整数),然后询问事件(作为一个单词字符串)。将整数和字符串传递给Calendar对象的CreateEvent方法。用户应该能够输入0 - 5个事件。如果用户输入-1,则循环应该退出,您的应用程序应以表格格式打印出所有事件。您的程序不应允许用户输入星期几的无效值。某一天的1 – 5-1以外的任何输入都将被视为无效。

注意: 从用户获取整数时,您需要在Scanner对象上使用nextInt()方法。从用户获取字符串时,您需要在Scanner对象上使用next()方法。

到目前为止,这是我的代码..

//DRIVER CLASS

/**
 *
 * @author Rocky
 */

//imports scanner
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
        calendercalenderObject = new calender();

        //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) {
            //test for valid day of week
            if ((dayOfWeek>=1) && (dayOfWeek<=5)){
                //calls createEvent method in calender class and passes 2 variables
                calenderObject.createEvent(userEvent,dayOfWeek);
            }  else {
                //error message
                System.out.println("You have entered an invalid number");

                //user prompts
                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");

                //collect user input
                dayOfWeek = userInput.nextInt(); 

                //end data validity test
            }
            //end while loop
        }


        //prints array to screen
        int i=0;

        for (i=0;i<events.length;i++){
            System.out.println(events[i]);
        }
        //end main method
    }
}


/**
 *
 * @author Rocky
 */

//imports scanner
import java.util.Scanner;


//begin calender class
public class calender {

    //creates events array
    String[] events = new String[5];

    //begin calender class constructor
    public calender() {
        //Initializes array 
        String[] events = {"-No event planned-","-No event planned-","-No event planned-","-No event planned-","-No event planned-"};
        //end calender class constructor
}

    //begin createEvent method
    public String[] createEvent (String userEvent, int dayOfWeek){
        //Start switch test
        switch (dayOfWeek){
            case 1: 
                events[0] = ("Monday Appoinment:") + userEvent;
                break; 
            case 2: 
                events[1] = ("Tuesday Appoinment:") + userEvent;
                break; 
            case 3: 
                events[2] = ("WednsdayAppoinment:") + userEvent;
                break;
            case 4: 
                events[3] = ("Thursday Appoinment:") + userEvent;
                break;
            case 5: 
                events[4] = ("Friday Appoinment:") + userEvent;
                break;
            default:
                break;
                //End switch test   
        }
        //returns events array   
        return events;
        //end create event method   
    }
    //end calender class    
}

4 个答案:

答案 0 :(得分:2)

你的main方法不知道events是什么,因为在范围内没有名为events的局部变量,并且在主类上没有名为events的静态变量。您需要告诉它您正在寻找特定日历对象中的事件对象,如下所示:

System.out.println(calendarObject.events[0]);

这假设您的两个类在同一个包中,或者两者都没有声明包(因此它们都在默认包中)。如果这两个类在不同的包中,则必须将实例成员公开为public或创建一个访问它的公共方法,以便另一个查看它。

基于类的对象的想法是您可以使用类来创建多个对象。由于您只创建了日历类的一个对象,因此在此分配中可能无法做到这一点。要引用对象的成员,您需要指定要在其中查找该成员的对象。

答案 1 :(得分:1)

首先,它说不要硬编码大小,使用常量。所以做一个像这样的常数:

private static final int ARRAY_SIZE = 5;

并将您的声明更改为

String[] events = new String[ARRAY_SIZE];

在初始化数组时,使用for-counter循环将每个插槽设置为“-No event planned-”,而不仅仅是{some number of“-No event planned”s},因为这不是动态,并且不会随着你的恒定大小而改变。

此外,在构造函数中删除类型声明

String[] events - &gt; events

通过声明类型,你创建一个新的局部变量,而不是修改字段,因此数组中的槽都是null,因为你从未修改过它们,而是修改了这个局部变量中的字段。

答案 2 :(得分:0)

您在构造函数中声明了一个本地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
}

希望它有效

答案 3 :(得分:-1)

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    
}