问题描述
代码如下:importjava.util.*;classViewMonth{intmonth;intyear;ViewMonth(finalintdisplayMonth,finalintdisplayYear){month=displayMonth;year=displayYear;}privateStringcheckMonth(){String[]months={//定义月的数组"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};returnmonths[month];}privateintcheckDays(){int[]numofDays={31,28,31,30,31,30,31,31,30,31,30,31//1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月};returnnumofDays[month];}/***使用此方法打印该月的日历.*/voidprintMonth(){/*将该月份起始处的天数留空.*/intinitialSpaces=0;try{/*获取月份名称.*/StringmonthName=checkMonth();System.out.println();System.out.println("ttt"+year+"年"+monthName);System.out.println();}catch(ArrayIndexOutOfBoundsExceptionae){System.out.println("超出范围..........");System.exit(0);}GregorianCalendarcal=newGregorianCalendar(year,month,1);System.out.println("t日t一t二t三t四t五t六");initialSpaces=cal.get(Calendar.DAY_OF_WEEK)-1;/*获取天数.*/intdaysInMonth=checkDays();/*检查是否为闰年并为二月增加一天.*/if(cal.isLeapYear(cal.get(Calendar.YEAR))&&month==1){++daysInMonth;}for(intctr=0;ctr<initialSpaces;ctr++){System.out.print("t");}for(intctr=1;ctr<=daysInMonth;ctr++){/*为单个日期添加空格.*/if(ctr<=9){System.out.print("");}System.out.print("t"+ctr);/*检查行的末尾.*/if((initialSpaces+ctr)%7==0){System.out.println();}else{System.out.print("");}}System.out.println();}}classaa{privatevoidshuru(){/*intyear,month;Scannersc=newScanner(System.in);System.out.print("请输入年份:");year=sc.nextInt();Scannersca=newScanner(System.in);System.out.print("请输入月份:");month=sca.nextInt();if(month>12||month<1){System.out.println("输入非法月份~!请重新输入~!");shuru();}*/}publicstaticvoidmain(String[]args){//aaa=newaa();intmonth,year;Scannersc=newScanner(System.in);System.out.print("请输入年份:");year=sc.nextInt();Scannersca=newScanner(System.in);System.out.print("请输入月份:");month=sca.nextInt();if(month>12||month<1){System.out.println("输入非法月份~!请重新输入~!");}//a.shuru();if(args.length==2){System.out.println("显示日历");System.out.println();intmon=Integer.parseInt(args[0]);month=mon-1;year=Integer.parseInt(args[1]);}else{Calendartoday=Calendar.getInstance();month=today.get(Calendar.MONTH);year=today.get(Calendar.YEAR);}ViewMonthmv=newViewMonth(month,year);mv.printMonth();}}请问下一,怎么老是显示当前时间!还有,当输入错误月份时候,怎么返回重新输入月份。在线等或者QQ练习562162946
解决方案
解决方案二:
好像是万年历哦!可以去我博客看一下,有源码·http://blog.csdn.net/Hamber_Bao
解决方案三:
加个returnif(month>12||month<1){System.out.println("输入非法月份~!请重新输入~!");return;}
解决方案四:
这一段判断不对,注释掉就可以了//if(args.length==2){////System.out.println("显示日历");//System.out.println();////intmon=Integer.parseInt(args[0]);//month=mon-1;//year=Integer.parseInt(args[1]);////}//else{//System.out.println("不显示日历");//Calendartoday=Calendar.getInstance();//month=today.get(Calendar.MONTH);//year=today.get(Calendar.YEAR);//}
解决方案五:
对了,这里要改下month=sca.nextInt()-1;