java求13号星期五

2022/2/2 20:42:25

本文主要是介绍java求13号星期五,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

看到外国人怕13号和星期五重叠,于是想看看什么时候会重叠,但没有现成的,于是写了点代码算了一下

public class Firday13 {

    public static int dx[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    public static int dy[] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };

    /**
     *闰年判断
     */
    public static boolean isYun(Integer year){
        if (year%400 == 0 ||(year%4 == 0 && year%100 == 0) ){
            return  true;
        }else {
            return false;
        }
    }

    public static void firday(Integer year ,Integer week){
        int a[] ;
        //循环月份
        if (isYun(year)){
            a = dy;
        }else {
            a = dx;
        }
        for (int i = 1; i < 12; i++) {

            for (int j = 1; j <= a[i]; j++,week++) {
                if (week>7){
                    week = week%7;
                }
                if (week == 5 && j == 13){
                    System.out.println(year+"年"+i+"月"+j+"日"+"是星期五");
                }
            }
        }

    }

    /**
     * 0是星期天,返回星期几
     */
    public static int getWeek(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,0,1);
        int x = calendar.get(calendar.DAY_OF_WEEK)-1;

        return x;
    }
    
    public static void main(String[] args) {
        for (int i = 1990; i <2100 ; i++) {
            firday(i,getWeek(i));
        }
    }
}

 看了两年是准的,一年一两次出现



这篇关于java求13号星期五的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程