老师没怎么讲,让我们自己查api文档,但是有的地方还是看不懂,疑问在例程的注释部分,求助高手指点迷津,不胜感觉!
import java.util.Calendar;
import java.util.GregorianCalendar;class ViewMonth {
    
    int month;   
    int year;   
    ViewMonth(final int displayMonth, final int displayYear) {
        month = displayMonth;
         year = displayYear;
    }   
    private String checkMonth() {        String[] months = {
            "1 月" , "2 月" , "3 月",
            "4 月"   , "5 月"      , "6 月",
            "7 月"    , "8 月"   , "9 月",
            "10 月" , "11 月" , "12 月"
        };        return months[month];
    }  
    private int checkDays() {         int[] numofDays = {
                31, 28, 31,
                30, 31, 30,
                31, 31, 30,
                31, 30, 31
         };         return numofDays[month];
    } 
    void printMonth() {        
        int initialSpaces = 0;        try {
      
           String monthName = checkMonth();
           System.out.println();
           System.out.println("\t\t\t " + year + " 年 " + monthName );
           System.out.println();        } catch (ArrayIndexOutOfBoundsException ae) {
            System.out.println("超出范围 ..........");
            System.exit(0);
        }        GregorianCalendar cal = new GregorianCalendar(year, month, 1);        System.out.println("\t日\t一\t二\t三\t四\t五\t六");
        initialSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;//cal.get(Calendar.DAY_OF_WEEK)什么意思?为什么这一行能得到日历第一行应该空格的字符数?       
        int daysInMonth = checkDays();        
        if (cal.isLeapYear(cal.get(Calendar.YEAR)) && month == 1) {
            ++daysInMonth;
        }        for (int ctr = 0; ctr < initialSpaces; ctr++) {
            System.out.print("\t");
        }        for (int ctr = 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();
    }
}
class MonthTest {    protected MonthTest() {
    }       public static void main(String[] args) {        int month, year;        if (args.length == 2) {
            System.out.println("显示日历");
            System.out.println();
            int mon = Integer.parseInt(args[0]);
            month = mon - 1;
            year = Integer.parseInt(args[1]);
        } else {
            Calendar today = Calendar.getInstance();
            month = today.get(Calendar.MONTH);
            year = today.get(Calendar.YEAR);
        }        ViewMonth mv = new ViewMonth(month, year);
        mv.printMonth();    }
}

解决方案 »

  1.   

    cal.get(Calendar.DAY_OF_WEEK)  
    得到的是这一天位于一周中的第几天 给一位数的日期号加个空格?有什么用?注释掉输出结果不变 
    这个只是排版问题吧 
      

  2.   

    允许我这个菜鸟问一下啊:为什么没有Public 类 啊?
      

  3.   

    不好意思,是public 类,不应该是Public 类,JAVA对大小写很敏感的,是吧?
      

  4.   


    没有publlic 也可以的   
         那就是默认咯。。