找不到符号//其他类中的变量

时间:2019-02-23 19:24:56

标签: java

所以这是我第一次在这里发布文章,希望你们能够为我提供帮助,反正这是我的代码:

    System.out.println("Would you like to run? (enter y to run): ");
    Scanner scan = new Scanner(System.in);
    char response = scan.next().charAt(0);
    if(response == 'y' || response == 'Y'){
        MyCalendar test1 = new MyCalendar();
        System.out.println("Enter year, month, and day, separated by space: ");
        int year = scan.nextInt();
        int month = scan.nextInt();
        int day = scan.nextInt();

        if(MyCalendar.isValidDate(year,month, day)){
            System.out.println("The above date is a valid date.");
            System.out.println("Day of that day in its week: "+ MyCalendar.dayOfWeek);
            System.out.println("Day of the date in its year: "+ MyCalendar.dayOfYear);
            return;
        }
        else{
            System.out.println("The above date is not valid.");
            return;
        }

    }
}

}

在我放置MyCalendar.dayOfWeek和MyCalendar.dayOfYear的行上,它表示找不到变量,它在另一个类中。

如何正确引用此方法?

MyCalendar类:

/**
 * Check for validity of a date 
 * 
 * @param   year      int (>= 1900 and <= 2100)
 * @param   month     int
 * @param   day       int
 * @return  booolean  true if valid date; false, otherwise
 */

public static boolean isValidDate(int year, int month, int day){
    return(year > 1900 && year <= 2100 && month > 0 && month < 13 && day > 0 
&& day < 32);
}

/**
 * Check for leap year
 * 
 * @param   year     int (>= 1900 and <= 2100)
 * @return  boolean  true if leap year; false, otherwise
 */

 private boolean isLeapYear(int year){
    return(year % 4 == 0 && year % 100 !=0 || year % 400 == 0);
}

/**
 * Compute the number of days in a valid month of a valid year  
 * 
 * @param   year   int (>= 1900 and <= 2100)
 * @param   month  int (range 1-12)
 * @return  int    the number of days in the given month of the year
 */
private int numDays(int year, int month, int day){
    int daysInMonth = 0;
    switch (month){
        case 1: daysInMonth = 31; break;
        case 2: if(isLeapYear(year))
            daysInMonth = 29; 
        else
            daysInMonth = 28;
        break;
        case 3: daysInMonth = 31; break;
        case 4: daysInMonth = 30; break;
        case 5: daysInMonth = 31; break;
        case 6: daysInMonth = 30; break;
        case 7: daysInMonth = 31; break;
        case 8: daysInMonth = 31; break;
        case 9: daysInMonth = 30; break;
        case 10: daysInMonth = 31; break;
        case 11: daysInMonth = 30; break;
        case 12: daysInMonth = 31; break;
    }
    return daysInMonth;
}

/**
 * Compute the day of the year for a valid date  
 * 
 * @param   year   int (>= 1900 and <= 2100)
 * @param   month  int (range 1-12)
 * @param   day    int (range 1-numDays(year, month))
 * @return  int    the day of the year
 */    
public int dayOfYear(int year,int month,int day){
    int dayOfYear = day;
    if(isLeapYear(year)){
        switch (month){
            case 1:
            dayOfYear = day; break;
            case 2:
            dayOfYear += 31; break;
            case 3:
            dayOfYear += 60; break;
            case 4:
            dayOfYear += 91; break;
            case 5:
            dayOfYear += 121; break;
            case 6:
            dayOfYear += 152; break;
            case 7:
            dayOfYear += 182; break;
            case 8:
            dayOfYear += 213; break;
            case 9:
            dayOfYear += 244; break;
            case 10:
            dayOfYear += 274; break;
            case 11:
            dayOfYear += 305; break;
            case 12:
            dayOfYear += 335; break;
        }}
    else{ 
        switch (month){
            case 1:
            dayOfYear = day; break;
            case 2:
            dayOfYear += 31; break;
            case 3:
            dayOfYear += 59; break;
            case 4:
            dayOfYear += 90; break;
            case 5:
            dayOfYear += 120; break;
            case 6:
            dayOfYear += 151; break;
            case 7:
            dayOfYear += 181; break;
            case 8:
            dayOfYear += 212; break;
            case 9:
            dayOfYear += 243; break;
            case 10:
            dayOfYear += 273; break;
            case 11:
            dayOfYear += 304; break;
            case 12:
            dayOfYear += 334; break;
        }}
    return dayOfYear;
}

/**
 * Compute the day of the week for a valid date  
 * 
 * @param   year    int (>= 1900 and <= 2100)
 * @param   month   int (range 1-12)
 * @param   day     int (range 1-numDays(year, month))
 * @return  String  the day of the week
 */        
public String dayOfWeek(int year, int month, int day, String t){
    int m=month;
    int y=year;
    int d=day;
    if (month > 3){
        m += 12;
        y -= 1;
    }
    int y0 = y - (14 - m) / 12;
    int x = y0 + y0/4 - y0/100 + y0/400;
    int m0 = m + 12 * ((14 - m) / 12) - 2;
    int dayOfWeek = (d + x + (31*m0)/12) % 7;

    switch (dayOfWeek) {
        case 0: 
        t = "Sunday";
        case 1:
        t = "Monday";
        case 2:
        t = "Tuesday";
        case 3:
        t = "Wednesday";
        case 4:
        t = "Thursday";
        case 5:
        t = "Friday";
        case 6:
        t = "Saturday";

    }
    return t;
}

}

编辑:不好意思,我正在参加初学者计算机科学课。谢谢大家的答复!

2 个答案:

答案 0 :(得分:1)

如果这是一种方法,那么您应该编写MyCalendar.dayOfWeek()和MyCalendar.dayOfYear(),不是吗?如果它们是变量,则检查它们是否在MyCalendar中公开。

编辑: 另外,由于您创建了MyCalendar实例,因此您可能还想执行test1.dayOfWeek()之类的操作。正如某人在评论中写道,最好还向我们展示MyCalendar类。

答案 1 :(得分:1)

我不知道您的MyCalender类的样子,但是我很确定您其中没有静态变量dayOfWeek。 您可能需要使用该类的对象。

System.out.println("Day of that day in its week: "+ test1.dayOfWeek(year, month, day));
System.out.println("Day of the date in its year: "+ test1.dayOfYear(year, month, day));