基本数学问题:判断闰年

本文介绍了判断闰年的规则,并提供了相应的示例函数及完整代码。内容包括公元年份如何根据除以4、100和400的条件判断是否为闰年,以及在2000年至3000年间的闰年输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、闰年的判断规则:

  1. 公元年分除以4不可整除,为平年。 
  2. 公元年分除以4可整除但除以100不可整除,为闰年。 
  3. 公元年分除以100可整除但除以400不可整除,为平年。 
  4. 公元年分除以400可整除,为闰年。

二、判断闰年的示例函数:

boolean isLeapyear(int year){
    if(((year%4 == 0) && (year%100 != 0)) || year%400 == 0){
        return true;        // 是闰年返回true
    }else return false;     // 不是闰年返回false
}

三、示例完整代码:

需求:输出2000年至3000年之间所有的闰年

public class Leapyear {
    // 公元年分除以4不可整除,为平年。 
    // 公元年分除以4可整除但除以100不可整除,为闰年。 
    // 公元年分除以100可整除但除以400不可整除,为平年。 
    // 公元年分除以400可整除,为闰年。
    public static boolean isLeapyear(int year){
        if(((year%4 == 0) && (year%100 != 0)) || year%400 == 0){
            return true;        // 是闰年返回true
        }else return false;     // 不是闰年返回false
    }

    public static void main(String[] args) {
        int count = 0;
        System.out.println("2000年到3000年的闰年如下:");
        for(int y = 2000; y <= 3000; y++){
            if(isLeapyear(y)){
                System.out.print(y + "\t");
                count++;
                if(count%10 == 0)
                    System.out.println();
            }
        }
    }
}

运行代码结果如下:

2000年到3000年的闰年如下:
2000	2004	2008	2012	2016	2020	2024	2028	2032	2036	
2040	2044	2048	2052	2056	2060	2064	2068	2072	2076	
2080	2084	2088	2092	2096	2104	2108	2112	2116	2120	
2124	2128	2132	2136	2140	2144	2148	2152	2156	2160	
2164	2168	2172	2176	2180	2184	2188	2192	2196	2204	
2208	2212	2216	2220	2224	2228	2232	2236	2240	2244	
2248	2252	2256	2260	2264	2268	2272	2276	2280	2284	
2288	2292	2296	2304	2308	2312	2316	2320	2324	2328	
2332	2336	2340	2344	2348	2352	2356	2360	2364	2368	
2372	2376	2380	2384	2388	2392	2396	2400	2404	2408	
2412	2416	2420	2424	2428	2432	2436	2440	2444	2448	
2452	2456	2460	2464	2468	2472	2476	2480	2484	2488	
2492	2496	2504	2508	2512	2516	2520	2524	2528	2532	
2536	2540	2544	2548	2552	2556	2560	2564	2568	2572	
2576	2580	2584	2588	2592	2596	2604	2608	2612	2616	
2620	2624	2628	2632	2636	2640	2644	2648	2652	2656	
2660	2664	2668	2672	2676	2680	2684	2688	2692	2696	
2704	2708	2712	2716	2720	2724	2728	2732	2736	2740	
2744	2748	2752	2756	2760	2764	2768	2772	2776	2780	
2784	2788	2792	2796	2800	2804	2808	2812	2816	2820	
2824	2828	2832	2836	2840	2844	2848	2852	2856	2860	
2864	2868	2872	2876	2880	2884	2888	2892	2896	2904	
2908	2912	2916	2920	2924	2928	2932	2936	2940	2944	
2948	2952	2956	2960	2964	2968	2972	2976	2980	2984	
2988	2992	2996

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值