在Java Eclipse中测试Julian Date

时间:2013-08-27 22:08:33

标签: java

我在尝试查看Eclipse的Julian日期时遇到了问题。测试的(日)月份为红色。有人能提供帮助吗?非常感谢。

public static boolean validDate (int day, int month, int year){
    if(month >=1 && month<=12 && day>=1 && day <=31) {
        if (day>30) {
            return false;
        }
    }

    if(month==2){
        if (leapYear(year)!= true&&day>28){
            return false;
        }
    }

    return true;
}

//Array to store total days in month, not leap year
public static int julianDate(int day, int month, int year){
    int m[] = {31,28,31,30,31,30,31,31,30,31,30,31};    
    int total = 0; // to store total days

    // loop to add the number of days in each month, previous to the month chosen
    for(int i = 0; i < month - 1; i++){ 
        total = total + m[i]; //adding value in array at index position i to total variable
    }

    // checking if the month entered is after feb and also leap year
    if(month  > 2 && leapYear(year)){
        //total holds days in previous months and adds days in current month - plus 1 as it is condition of leapyear
        return total + day + 1; 
    }else{
        // no added 1 as the else is not leap year
        return total + day; 
    }

}

测试

else if (menuNumber == 2);
        System.out.println("Enter Julain Date DD/MM/YYYY"); //Here he print out Julain Date and asks for specific format dd/mm/yyyy

        int day, month, year = in.nextInt();
        System.out.println();

        if (newDate.validDate(day, month, year) ) {
            System.out.println(newDate.validDate(day, month, year));
        }else{
            System.out.println("Not a Date");
        }

1 个答案:

答案 0 :(得分:0)

我在 TESTING 代码中看到的两件事:

1)你可能实际上并不想要分号

else if (menuNumber == 2);

但是如果没有看到剩下的那个 - 否则我不能百分百肯定。

2)在行

int day, month, year = in.nextInt();

您只是实例化year。您没有实例化daymonth。这可能会导致错误,具体取决于您的Eclipse设置。 (而且你可能想要将它们实例化为某种东西)。如果Eclipse认为该部分导致错误,Eclipse将用红色下划线代码。