问题描述
要求用户输入年份,直接输出全年的日期,自己写了一些。。希望大家指教!!publicclassMayday{intyear;intmonth;publicbooleanisRun(inta){if(a%4==0&&a%100!=0||a%400==0){returntrue;}else{returnfalse;}}publicintyday(){intsum=0;for(inti=1900;i<year;i++){if(isRun(i)){sum+=366;}else{sum+=365;}}returnsum;}publicintmday(){intsum1=0;for(inti=1;i<month;i++){switch(i){case6:case4:case9:case11:sum1+=30;break;case2:if(isRun(i)){sum1+=29;}else{sum1+=28;}break;default:sum1+=31;break;}}returnsum1;}publicvoidshou(){inta=yday();intb=mday();intzday=(a+b+1)%7;System.out.println("日t"+"一t"+"二t"+"三t"+"四t"+"五t"+"六t");for(inti=0;i<zday;i++){System.out.print("t");}intsum1=0;switch(month){case6:case4:case9:case11:sum1+=30;break;case2:if(isRun(month)){sum1+=29;}else{sum1+=28;}break;default:sum1+=31;break;}for(inti=1;i<=sum1;i++){System.out.print(i+"t");if((i+zday)%7==0){System.out.println();}}}}
解决方案
解决方案二:
packageDK_Date;importjava.util.Date;publicclassDK_Date{publicintYear;//年份publicintMonth;//月份publicintDay;//日期publicintDays;//当月有几天publicintWeek;//当月第一天为周几publicvoidgetCurDate(){Datedate=newDate();this.Year=date.getYear()+1900;this.Month=date.getMonth()+1;this.Day=date.getDate();}/***判断是否为闰年**@paramyear*@return如果为真则为闰年,反之为平年*/publicbooleanisRun(intyear){if((year%4==0&&year%100!=0)||year%400==0)returntrue;returnfalse;}/***计算当月天数**@parammonth*月份*@paramyear*年份*/publicvoidgetDays(intmonth,intyear){switch(month){case2:if(isRun(year))this.Days=29;elsethis.Days=28;break;case4:case6:case9:case11:this.Days=30;break;default:this.Days=31;break;}}/***计算当月第第一天是一年中的第几天**@parammonth*@paramyear*@return*/publicintgetYearDays(intmonth,intyear){intdays=1;for(inti=1;i<month;i++){switch(i){case2:if(isRun(year))days+=29;elsedays+=28;break;case4:case6:case9:case11:days+=30;break;default:days+=31;break;}}returndays;}/***计算1900年到当年1月1日经过了多少天**@paramyear*当前年份*@return*/publicintgetYearsDay(intyear){intdays=0;for(inti=1900;i<year;i++){if(isRun(i))days+=366;elsedays+=365;}returndays;}/***计算当月第一天为周几**@paramyear*年份*@parammonth*月份*/publicvoidgetWeek(intyear,intmonth){intdays=getYearsDay(year)+getYearDays(month,year);this.Week=days%7==0?7:days%7;}}packageDK_Date;importjava.util.Scanner;publicclassDateUI{privateDK_Datedate;Scannerscanner=newScanner(System.in);publicvoidinitial(){date=newDK_Date();date.getCurDate();date.getWeek(date.Year,date.Month);date.getDays(date.Month,date.Year);}publicvoidshow(){System.out.println("◆◇◆◇◆◇DK万年历◆◇◆◇◆◇n");System.out.println("北京时间:"+date.Year+"年"+date.Month+"月"+date.Day+"日n");getShow(date.Week,date.Days);while(true){System.out.println("n请输入年份:");date.Year=scanner.nextInt();System.out.println("请输入月份:");date.Month=scanner.nextInt();date.getWeek(date.Year,date.Month);date.getDays(date.Month,date.Year);getShow(date.Week,date.Days);}}publicvoidgetShow(intweek,intdays){System.out.println("周日t周一t周二t周三t周四t周五t周六t");if(week!=7){for(inti=0;i<week;i++){System.out.print("t");}}for(inti=1;i<=days;i++){System.out.print(i+"t");if(i%7==7-week){System.out.println();}}}}packageDK_Date;publicclassStart{publicstaticvoidmain(String[]args){DateUIdate=newDateUI();date.initial();date.show();}}