2013-08-27 40 views
0

我遇到了嘗試查找Eclipse的Julian日期的測試問題。測試中的(每月)高亮紅色。任何人都可以幫忙嗎?非常感激。在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"); 
     } 
+0

你有沒有考慮過使用[JodaTime](http://www.joda.org/joda-time/)? – GGrec

+0

我沒有,因爲我不知道如何使用它,也需要做到這一點具體方式 –

回答

0

兩件事情我看到你測試代碼:

1)你可能實際上並不希望在

分號
else if (menuNumber == 2); 

但沒有看到其餘的,如果 - 否則我不能100%肯定。

2)行

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

你只實例year。您沒有實例化daymonth。這可能會導致錯誤,具體取決於您的Eclipse設置。 (並且您可能想要將它們實例化爲某種東西)。如果Eclipse認爲該部分導致錯誤,Eclipse會將其中一段代碼加下劃線。

+0

歡呼傢伙得到它排序 –