判断闰年(java语言编写)

输入一个年份,判断该年是不是闰年
若一个年份是闰年,则该年份能被4整除,但是不能被100整除,或者该年份能被100整除的同时也能被400整除。

import java.util.Scanner;

public class Exercise{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int year;
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入年份:");
        year=sc.nextInt();
        if(judge(year))
            System.out.println("该年份是闰年");
        else
            System.out.println("该年份不是闰年");
    }
    public static boolean judge(int y){
        if((y%4==0&&y%100!=0)||(y%100==0&&y%400==0))
            return true;
        else
            return false;
    }
}

猜你喜欢

转载自blog.csdn.net/ws_PersonalSpace/article/details/81366950