【20171002】Java每日一练

来源:互联网 发布:知豆能不能上高速 编辑:程序博客网 时间:2024/05/03 00:17

【插播】已知年月日求星期(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。");        }    }}

这里写图片描述

代码实现2:蔡勒公式

这里写图片描述

原创粉丝点击