欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【20171002】Java每日一练

程序员文章站 2022-07-12 08:56:32
...

【插播】已知年月日求星期(1900年之后)

代码实现1:



package c2;

import java.util.Scanner;

public class C2_03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int month;
        int mmonth;
        int year;
        int date;
        int day;
        long total = 0;

        System.out.println("请输入年份:");
        Scanner sc3 = new Scanner(System.in);
        year = sc3.nextInt();

        System.out.println("请输入月份:");
        Scanner sc = new Scanner(System.in);
        month = sc.nextInt();

        System.out.println("请输入日期:");
        Scanner sc2 = new Scanner(System.in);
        date = sc2.nextInt();




        for(int j = 1900; j <= year ; j++)
        {
            if(((j % 100 != 0) && (j % 4 == 0)) || (j % 400) == 0){
                total+=1;
            }

        }
        total = total +(year - 1900) * 365 + date;


        for(mmonth = month -1;mmonth >=1;mmonth --){

                if(mmonth == 1)
                {
                     total += 31;continue;
                }    
                if(mmonth <= 2){
                    if(((year % 100 != 0) && (year % 4 == 0)) || (year % 400) == 0){

                            total +=29;continue;
                        }
                    else{
                            total += 28;continue;
                        }

                        }

                if(mmonth <= 3)
                {
                    total += 31;continue;
                }

                if(mmonth <= 4)
                {
                    total += 30;continue;
                }

                if(mmonth <= 5)
                {
                    total += 31;continue;
                }
                if(mmonth <= 6)
                {
                    total += 30;continue;
                }
                if(mmonth <= 7)
                {
                    total += 31;continue;
                }
                if(mmonth <= 8)
                {
                    total += 31;continue;
                }
                if(mmonth <= 9)
                {
                    total += 30;continue; 
                }
                if(mmonth <= 10)
                {
                    total += 31;continue;
                }
                if(mmonth <= 11 )
                {
                    total += 30;continue;
                }



        }



        System.out.println("这一天是第" + total + "天");

        day = (int)(total % 7);
        //System.out.println("今天是星期" + day);

        switch(day)
        {
        case 1:
            System.out.println("这一天是星期1。");
            break;
        case 2:
            System.out.println("这一天是星期2。");
            break;
        case 3:
            System.out.println("这一天是星期3。");
            break;
        case 4:
            System.out.println("这一天是星期4。");
            break;
        case 5:
            System.out.println("这一天是星期5。");
            break;
        case 6:
            System.out.println("这一天是星期6。");
            break;
        default:
            System.out.println("这一天是星期7。");

        }



    }

}

【20171002】Java每日一练

代码实现2:蔡勒公式

【20171002】Java每日一练