English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java complete list of examples
Dans ce programme, vous apprendrez à vérifier si une année donnée est une année bissextile. Utilisez les instructions if else.
L'année bissextile peut être4divisible, mais se terminant par 00, l'année centuriale est exceptée. Seulement si elle est divisible400 divisible, l'année centuriale est une année bissextile
public\ class\ LeapYear\ { public\ static\ void\ main(String[]\ args)\ { int\ year\ = 1900; boolean\ leap\ =\ false; if(\ year\ % 4 ==\ 0) { if(\ year\ % 100 == 0) { //a year can be divided by400 is divisible, therefore it is a leap year if ( year % 400 == 0) leap = true; else leap = false; } else leap = true; } else leap = false; if(leap) System.out.println(year + "is a leap year."); else System.out.println(year + "is not a leap year."); } }
The output when running the program is:
1900 is not a leap year.
Change the value of year to2012The output is:
2012 is a leap year.
In the above program, the given year1900 is stored in the variable year.
because1900 is divisible by4divisible and is a century year (ending with 00), while leap years can be4divisible by 00. Because1900 cannot be4divisible by 00, so1900 is not a leap year.
However, if we change year to2000, then it can be4divisible and is a century year, and can also be divided by4divisible by 00. Therefore,2000 is a leap year.
Similarly, if we change the year to2012then the year can be divided by4divisible and not a century year, so2012is a leap year. We do not need to check2012a year can be divided by400 is divisible by.