java写万年历_java怎么编写输入万年历

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

我的代码很简单 楼主一定看的懂吧

import javax.swing.JOptionPane;

class Calendar

{

public int year;

public int month;

public int alldays;//所有的天数;

public int weekDay;//一月当中的第一天是星期几;

public int monthDays;//每月的天数

public boolean isRn;//判断是否闰年

public Calendar()

{

do

{

String Year=JOptionPane.showInputDialog("请输入年份,必须大于1900年! ");

year=Integer.parseInt(Year);

}

while(year<1900);

do

{

String Month=JOptionPane.showInputDialog("请输入月份 ,必须在1~12之间!");

month=Integer.parseInt(Month);

}

while(!(month>=1&&month<=12));

isRn=year%4==0&&year%100!=0||year%400==0;

System.out.println("\t\t "+year+"年"+month+"月的日历如下\n");

System.out.println("-------------------欢迎使用万年历-------------------\n");

}

public void getDays()

{

for(int i=1900;i

{

if(i%4==0&&i%100!=0||i%400==0)

alldays +=366;

else

alldays +=365;

}

for (int i=1;i

{

switch(i)

{

case 4:

case 6:

case 9:

case 11:

alldays +=30;

monthDays=31;

break;

case 2:

if(isRn)

{

alldays +=29;

monthDays=31;

}

else

{

alldays +=28;

monthDays=31;

}

break;

default:

alldays +=31;

if(month==2)

{

if(isRn)

monthDays=29;

else monthDays=28;

}

else if(month==8)

monthDays=31;

else

monthDays=30;

break;

}

}

if(month==1)

monthDays=31;

weekDay=(alldays+1)%7;

}

public void printCalendar()

{

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

for (int i=0;i

{

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

}

for(int i=1;i<=monthDays;i++)

{

if(i<=9)

System.out.print(" ");

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

if((i+weekDay)%7==0)

{

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

}

}

}

}

public class TestCalendar

{

public static void main(String[] args)

{

Calendar cald=new Calendar();

cald.getDays();

cald.printCalendar();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值