读书人

月历小程序 In java

发布时间: 2012-12-26 14:39:28 作者: rapoo

万年历小程序 In java

最近想到大学期间写的一个万年历小程序,写出来之后还很自豪。现在想想当时的程序逻辑混杂,结构紊乱,就在空闲时间重新写了个。本来以为二十多分钟就能写好了,结果写了一个多小时。。拿出来给我sdu大一的学弟学妹们参考一下吧。

?

ps:没有加入输入异常的处理,即默认输入正确的年份格式,如:2000。另外,最早查询年份到(含)1950年。

?    1 楼    闪闪4521    2012-10-11              干嘛要在main里面写那么多东西呢?    2 楼    cs6641468    2012-10-11              借组Calendar类,五分钟就可以了    3 楼    hellostory    2012-10-11              使用Calendar可以减少很多代码,而且也容易很多!    4 楼    dts228    2012-10-11              public static void main(String argus[]) throws IOException  {
System.out.println("输入年:");
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String in = r.readLine();
Calendar c = Calendar.getInstance();
int inYear = Integer.parseInt(in);
c.set(Calendar.YEAR, inYear);
int day = 1;
int lastMouth = 0;
while (true) {
c.set(Calendar.DAY_OF_YEAR, day++);
if (c.get(Calendar.YEAR) > inYear) {
break;
}
int month = c.get(Calendar.MONTH) + 1;
int monthDay = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
if (lastMouth != month) {
System.out.println();
System.out.println(month + "月");
System.out.println("周日 周一 周二 周三 周四 周五 周六");
lastMouth = month;
}
if(monthDay == 1){
for (int i = 0; i < (week-1) % 7; i++) {
System.out.print(" ");
}
}
if (monthDay < 10) {
System.out.print(" "+monthDay);
} else {
System.out.print(" "+ monthDay);
}
if(week % 7 == 0){
System.out.println();
}
}
} 5 楼 yunshu321 2012-10-22 cs6641468 写道借组Calendar类,五分钟就可以了
如四楼嘛?其实也不是简单多少吧。。亲来写个试试

读书人网 >编程

热点推荐