java 简易的万年历

import java.util.Scanner;
class RiLi 
{
public static void main(String[] args) 
{
//输入要查询的年份和月份
Scanner in = new Scanner(System.in);
int statue=1;
while(statue==1){
System.out.println("请输入你要查看的年份!");
int findYear = in.nextInt();
System.out.println("请输入你要查看的月份!");
int findMonth = in.nextInt();


//万年历起始时间
String s = "1900年1月1日星期一";

//计算出查询年的第一天是星期几
int i = 0,allDay = 0;
for(i=1900;i<findYear;i++){
if(i%4==0 && i%100!=0 || i%400==0){
allDay += 366;
}
else{
allDay += 365;
}
}


//计算查询月的第一天是星期几
//判断该年为平年还是闰年
int weekday = 0;
int yearType = 0;//0为平年 365天 2月28天,1为闰年 366天 2月29天
int count = 0;
if(findYear%4==0 && findYear%100!=0 || findYear%400==0){
switch(findMonth){
case 2:
count = 31;
break;
case 4:
case 6:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 -2) + 29;
break;
case 9:
case 11:
count = 31 * (findMonth / 2 +1) + 30 * (findMonth / 2 - 2) + 29;
break;
case 1:
count = 0;
break;
case 3:
case 5:
case 7:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 - 1) + 29;
break;
case 8:
case 10:
case 12:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 - 2) + 29;
break;
}
}
else{
switch(findMonth){
case 2:
count = 31;
break;
case 4:
case 6:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 -2) + 28;
break;
case 9:
case 11:
count = 31 * (findMonth / 2 +1) + 30 * (findMonth / 2 - 2) + 28;
break;
case 1:
count = 0;
break;
case 3:
case 5:
case 7:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 - 1) + 28;
break;
case 8:
case 10:
case 12:
count = 31 * (findMonth / 2) + 30 * (findMonth / 2 - 2) + 28;
break;
}
}
weekday = (count + allDay) % 7 + 1; 
System.out.println(weekday);

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

int flag=0;
if(findMonth == 1 || findMonth == 3 || findMonth == 5 || findMonth == 7 || findMonth == 8 || findMonth == 10 || findMonth == 12){
for(int k=1;k<=weekday;k++){
System.out.print("\t");
flag++;
}
for(int j=1;j<=31;j++){
if(flag%7==0){
System.out.println();
}
System.out.print(j+"\t");
flag++;
}
flag=0;
}


if(findMonth == 4 || findMonth == 6 || findMonth == 9 || findMonth == 11){
for(int k=1;k<=weekday;k++){
System.out.print("\t");
flag++;
}
for(int j=1;j<=30;j++){
if(flag%7==0){
System.out.println();
}
System.out.print(j+"\t");
flag++;
}
flag=0;
}


if(yearType == 1 && findMonth ==2){
for(int k=1;k<=weekday;k++){
System.out.print("\t");
flag++;
}
for(int j=1;j<=29;j++){
if(flag%7==0){
System.out.println();
}
System.out.print(j+"\t");
flag++;
}
flag=0;
}


if(yearType == 0 && findMonth ==2){
for(int k=1;k<=weekday;k++){
System.out.print("\t");
flag++;
}
for(int j=1;j<=28;j++){
if(flag%7==0){
System.out.println();
}
System.out.print(j+"\t");
flag++;
}
flag=0;
}
System.out.println();
System.out.println("退出输入0,继续输入1");
statue = in.nextInt();
}
}
  1. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值