我遇到了导入课程的问题,我不确定为什么会出错

时间:2015-03-16 05:29:03

标签: java

我有两个类,一个将被导入到另一个类中。在第一堂课中,唯一错误的是它没有将我的“枚举”识别为开头

`public class myDate {

    public enum Date {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};

    private Date Day;

    public myDate(Day**<--this is considered error** currentDay)
    {
        this.Day = currentDay;
    }`

我觉得这会影响导入此课程的能力。

这是我的测试课程。这是我写的代码。大部分都很好。

import java.util.Scanner;
import javax.swing.*;
import myDate.Date;

public class testDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame("Message");
        Scanner keyboard = new Scanner(System.in);
        int enterDay;
        Date currentDay;
        Date nextDay;
        Date prevDay;
        Date calcDay;
        int userChoice;
        boolean loop;

        loop = true;

        myDate day = new myDate(Date.MONDAY);

        while(loop)
        {
            Object[] options = {
                    "Set The Day", "Print The Day", "Return The Day",
                    "Return The Next Day", "Return The Previous Day",   
                    "Calculate The Next Weekday"
        };

        userChoice = JOptionPane.showOptionDialog(frame, "Choose from the actions below", "What is The Day of The Week Game?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

        switch(userChoice)
        {
        case 0:
            System.out.print("Please enter a number 1-7, representing Monday-Sunday");
                enterDay = keyboard.nextInt();
                keyboard.nextLine();
                day.setDay(enterDay);
                break;

        case 1:
            currentDay = day.getDay();
            day.showCurrentDay(currentDay);
            break;

        case 2:
            currentDay = day.getDay();
            day.showCurrentDay(currentDay);
            break;

        case 3:
            currentDay = day.getNextDay();
            day.showCurrentDay(nextDay);
            break;

        case 4:
            prevDay = day.getPrevDay();
            day.showCurrentDay(prevDay);
            break;

        case 5:
            System.out.print("Please enter the number of days you want to add to calculate the new weekday ");
            enterDay = keyboard.nextInt();
            keyboard.nextLine();
            calcDay = day.calcDay(enterDay);
            day.showCurrentDay(calcDay);
            break;
        }

    System.out.print("Would you like to continue? Enter 1 for 'Yes' and 2 for 'No'");
        enterDay = keyboard.nextInt();
        keyboard.nextLine();

        if (enterDay == 2)
        {
            loop = false;
            System.out.print("YOU SHALL PASS");
        }

        keyboard.close();

        System.out.print("YOU SHALL NOT PASS");

        }


    }

}
}

以下是提出错误的部分。所有变量分配给'Date'和导入'myDate.Date;'标有红色,我不知道为什么。

 import myDate.Date;  <--- this part comes up as error

    public class testDate {

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            JFrame frame = new JFrame("Message");
            Scanner keyboard = new Scanner(System.in);
            int enterDay;
            Date currentDay;
            Date nextDay;
            Date prevDay;
            Date calcDay;
            int userChoice;
            boolean loop;

            loop = true;

            myDate day = new myDate(Date.MONDAY);
        }

3 个答案:

答案 0 :(得分:1)

您不需要(也不能)从类Date导入字段myDate。您可以访问Date类{ - 1}}中的myDate枚举,例如

myDate.Date currentDay;
myDate.Date nextDay;
myDate.Date prevDay;
myDate.Date calcDay;

您还可以将enum移动到其自己的源文件中。我还要重命名它,已经有java.sql.Datejava.util.Date

答案 1 :(得分:1)

问题是你的构造函数错了:

public myDate(Day**<--this is considered error** currentDay)

应使用Day类型Date

public myDate(Date currentDay)

答案 2 :(得分:0)

您应该导入myDate类而不是其方法。 然后创建一个实例并访问其Date方法。

像,

import myDate;
....
myDate mD = new myDate();
mD.Date();

注意:类始终以大写字母和带小写字母的方法开头。