目錄
Day 4:條件語句實戰——閏年問題
一、基礎知識及代碼思路
二、代碼及測試
小結
Day 4:條件語句實戰——閏年問題
Task:
- if 語句的嵌套.
- 基本規律自行百度.
- 布爾類型.
一、基礎知識及代碼思路
1. 什么是閏年?
????????閏年是歷法中的名詞,分為普通閏年和世紀閏年。
????????閏年(Leap Year)是為了彌補因人為歷法規定造成的年度天數與地球實際公轉周期的時間差而設立的。補上時間差的年份為閏年。——來自百度百科
????????普通閏年:公歷年份是4的倍數,且不是100的倍數的,為閏年
????????世紀閏年:公歷年份是整百數的,必須是400的倍數才是閏年(
? ? ? ? 故根據定義,將判斷要點轉化為計算機語言為:
(i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)
2. 邏輯判斷——針對定義
? ? ? ? 從定義上來講,其實只需要一個判斷即可,即以下值為 true 還是 false:
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
3. 邏輯判斷——針對 if-else 分析
? ? ? ? 所有程序在碼碼前,都應該在大腦內建立一個初步的邏輯判斷流程圖。
? ? ? ? 在 if-else 語句中,尤其需要注意判斷的包含關系:即應該先判斷較為寬松的條件,后判斷較為嚴格的條件。梳理思路,可以得到如下流程圖:
? ? ? ? 當然,如果存在梳理困難的情況,我們可以借助韋恩圖來幫助我們判斷包含關系。
????????首先可以對于年份被4整除進行排除,如果年份無法被4整除,那么直接可以判斷不是閏年(圖中淺天藍色外圍)。若被4整除了,我們討論的范圍就進入的4倍數的區域,這個區域內要額外考慮100倍數問題(進入淺黃色區域)。
????????這里的else if語句不能直接用“余100”判斷,因為“余100”直接會包含的淺黃色及其內的肉色全部區域,這樣無法分離出內部的“400倍數”。
????????因此必須額外判斷年份是否被400整除,若可以(進入最內部的肉色區域),就是閏年。
????????所以,真正好的if-else if-else語句要正確包含韋恩圖最大的范圍與最小的范圍,并且從if到else過程可以映射為韋恩圖的從最大區域到最小區域的過程。
二、代碼及測試
? ? ? ? 需要說明的是,為了培養良好的代碼結構以及代碼習慣,我們這里依舊將判斷封裝為一個函數,即:
/************************ Is the given year leap?* * @param paraYear The given year.**********************/public static boolean isLeapYear(int paraYear) {if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {return true;} else {return false;} // Of if}// Of isLeapYear/************************ Is the given year leap? Replace the complex condition with a number of if.* * @param paraYear The given year.**********************/public static boolean isLeapYearV2(int paraYear) {if (paraYear % 4 != 0) {return false;} else if (paraYear % 400 == 0) {return true;} else if (paraYear % 100 == 0) {return false;} else {return true;} // Of if}// Of isLeapYearV2
? ? ? ? 整體代碼如下:
package basic;/*** The usage of sth.** @author: Changyang Hu joe03@foxmail.com* @date created: 2025-05-06*/
public class isLeapYear {/************************ The entrance of the program.* * @param args Not used now.**********************/public static void main(String args[]) {// Test isLeapYearint tempYear = 2021;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2000;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2100;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2004;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");// Test isLeapYearV2System.out.println("Now use the second version.");tempYear = 2021;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2000;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2100;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2004;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");}// Of main/************************ Is the given year leap?* * @param paraYear The given year.**********************/public static boolean isLeapYear(int paraYear) {if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {return true;} else {return false;} // Of if}// Of isLeapYear/************************ Is the given year leap? Replace the complex condition with a number of if.* * @param paraYear The given year.**********************/public static boolean isLeapYearV2(int paraYear) {if (paraYear % 4 != 0) {return false;} else if (paraYear % 400 == 0) {return true;} else if (paraYear % 100 == 0) {return false;} else {return true;} // Of if}// Of isLeapYearV2}// Of class LeapYear
? ? ? ? 運行結果如下:
小結
????????閏年計算是計算機語言關于條件語句與邏輯學習的典型案例,從中無論是初學者還是后來學習的人都應當深深體會程序語言邏輯的合理性已經對于問題的基本抽象。
????????雖然這是個非常基礎的計算機問題,但是以小見大,其中對于問題的抽象能力以及建立正確判斷邏輯的能力是貫通始終的。從最簡單的練習開始打牢基礎,才能在之后的學習生涯中游刃有余的處理更加復雜的問題。