2016-09-28 116 views
0
<script Language="c#" runat="server"> 
    void Page_Load() 
    { 
    int currentYear = DateTime.Now.Year(); 

    if (currentYear % 400 == 0) { 
    Message2.Text = ("This is a leap year"); 
    } 
    else { 
    Message2.Text = ("This is not a leap year"); 
    } 

} 

目前我遇到RunTime錯誤。我的目標是測試使用DateTime.Now.Year()的當前年份是否爲閏年。我認爲問題是我沒有正確地將年份轉換爲整數?請指教。檢查年份是否爲閏年

+2

['Year'](https://msdn.microsoft.com/en -us/library/system.datetime.year.aspx)是屬性而不是方法。所以,不需要調用括號 - 'int currentYear = DateTime.Now.Year;' –

+0

哦哇,這真是愚蠢的我...謝謝! – penmas

+2

你爲什麼要用400? 2016年是閏年,但2016/400 = 5.04 – Greg

回答

-1

年屬性,以便犯規需要括號...

0
int x= int.Parse(Console.ReadLine()); 

    if((x%400==0)||(x%100==0)||(x%4==0)) 
    { 
     Console.WriteLine(" \n\n\n The year is a leap year ...!"); 
    } 
    else 
    { 
      Console.WriteLine("\n\n\n The year is not a leap year"); 
    } 
+0

僅提供代碼解答。 – GhostCat

+0

謝謝。代碼已修改 – seyed