日期:2014-05-20  浏览次数:20759 次

新手求教打印日历项的问题
Java code

import java.util.*;

public class CalendarTest{
    public static void main(String[] args){
        GregorianCalendar d = new GregorianCalendar();
        int month = d.get(Calendar.MONTH);
        int today = d.get(Calendar.DAY_OF_MONTH);
        int weekday = d.get(Calendar.DAY_OF_WEEK);
        
        d.set(Calendar.DAY_OF_MONTH, 1);
        
        
        System.out.println("SUN MON TUE WED THU FRI SAT");
        for(int i=Calendar.SUNDAY;i<weekday;i++){
            System.out.print("   ");  //3个空格
        }
        
        do{
            int day = d.get(Calendar.DAY_OF_MONTH);
            System.out.printf("%3d",day);
            
            if(day==today){
                System.out.print("*");
            }
            else
                System.out.print(" ");
            
            if(weekday==Calendar.SATURDAY){
                System.out.println();
            }
            d.add(Calendar.DAY_OF_MONTH, 1);
            weekday = d.get(Calendar.DAY_OF_WEEK);
        }while(d.get(Calendar.MONTH)==month);
        
        if(weekday != Calendar.SUNDAY){
            System.out.println();
        }     //这个if语句的作用是?
        
    }
}



运行结果(2010.11.9)
SUN MON TUE WED THU FRI SAT
  1 2 3 4 5 6 
  7 8 9* 10 11 12 13 
 14 15 16 17 18 19 20 
 21 22 23 24 25 26 27 
 28 29 30 

java核心技术 上的一个例子,我以为是对齐的问题,但调试了几次,没解决,系统日期调到12月份貌似还好,请大家指导一下;另外最后面的那个if语句的作用是什么?
先谢了!

------解决方案--------------------
System.out.println();
} //这个if语句的作用是?

换行的作用