2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 万年历c语言打印年月 万年历(实现从键盘输入1900年之后的任意的某年 某月 输出该月

万年历c语言打印年月 万年历(实现从键盘输入1900年之后的任意的某年 某月 输出该月

时间:2019-08-17 19:24:24

相关推荐

万年历c语言打印年月 万年历(实现从键盘输入1900年之后的任意的某年 某月 输出该月

//25.万年历(实现从键盘输入1900年之后的任意的某年、某月,输出该月的日历)

import java.util.Scanner;

public class Calendar{

public static void main(String[] args){

//显示日历

// 1、对应月份的天数

//① 1、3、5、7、8、10、12月份 31天

//② 4、6、9、11 30天

//③ 2 28天或29天(待定)

// 2、对应月份1号 是星期几

//① 1900年 1月 1日 星期1

//② 指定日期的星期 与1900年1月1日相差的天数%7+1 (1~7的范围)

//③ 相差天数=1900年与年份之间相差的天数 + 年份到指定月份1号之前相差的天数

// 3、指定年份是平年还是闰年(确定二月 的天数)

//① 确定指定年份的2月有多少天

//接收用户从控制台输入的数据

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份(>=1900):");

int year = sc.nextInt();//获取年份

System.out.println("请输入月份[1-12]:");

int month = sc.nextInt();//获取月份

//1、对应月份的天数

int days = 28;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){

days = 31;

}else if(month==4||month==6||month==9||month==11){

days = 30;

}else{

if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况

days = 29;

}

}

//2、相差天数

int yDays = 0;//统计两个年份之间相差的天数

for(int y = 1900;y

if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况

yDays += 366;

}else{

yDays += 365;

}

}

int mDays = 0;//指定年份1月1日,到指定月份1日相差的天数

for(int m = 1;m

if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){

mDays += 31;

}else if(m==4||m==6||m==9||m==11){

mDays += 30;

}else{

if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况

mDays += 29;

}else{

mDays += 28;

}

}

}

int totalDays = yDays + mDays+1;//相差总天数

int week = totalDays%7+1;//指定月份1号 的星期

System.out.println("日\t一\t二\t三\t四\t五\t六");

//先根据星期 确定需要打印空位的 情况(week)

for(int i = 1;i<=week;i++){

if(i%7==0){

System.out.println(" \t");

}else{

System.out.print(" \t");

}

}

//可以打印对应月份的天数(days)

for(int j = 1;j<=days;j++){

if((j+week)%7==0){

System.out.println(j+"\t");

}else{

System.out.print(j+"\t");

}

}

}

}

万年历c语言打印年月 万年历(实现从键盘输入1900年之后的任意的某年 某月 输出该月的日历)...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。