Topic: 《JAVA2 核心技术 卷I》一个程序询问?

  Print this page

1.《JAVA2 核心技术 卷I》一个程序询问? Copy to clipboard
Posted by: 浦饭
Posted on: 2005-03-17 10:56

本书第96页,一个关于使用 GregorianCalendar类的例子中,倒数第六行:对于每一天,如果天<10,我们就先打印一个空格,然后打印天。这句是什么意思啊?为什么要求天<10?

1. import java.util.*;
2.
3. public class CalendarTest
4. {
5. public static void main(String[] args)
6. {
7. // construct d as current date
8. GregorianCalendar d = new GregorianCalendar();
9.
10. int today = d.get(Calendar.DAY_OF_MONTH);
11. int month = d.get(Calendar.MONTH);
12.
13. // set d to start date of the month
14. d.set(Calendar.DAY_OF_MONTH, 1);
15.
16. int weekday = d.get(Calendar.DAY_OF_WEEK);
17.
18. // print heading
19. System.out.println("Sun Mon Tue Wed Thu Fri Sat");
20.
21. // indent first line of calendar
22. for (int i = Calendar.SUNDAY; i < weekday; i++ )
23. System.out.print(" ");
24.
25. do
26. {

27. // print day
28. int day = d.get(Calendar.DAY_OF_MONTH);
29. if (day < 10) System.out.print(" ");
30. System.out.print(day);

31.
32. // mark current day with *
33. if (day == today)
34. System.out.print("* ");
35. else
36. System.out.print(" ");
37.
38. // start a new line after every Saturday
39. if (weekday == Calendar.SATURDAY)
40. System.out.println();
41.
42. // advance d to the next day
43. d.add(Calendar.DAY_OF_MONTH, 1);
44. weekday = d.get(Calendar.DAY_OF_WEEK);
45. }
46. while (d.get(Calendar.MONTH) == month);
47. // the loop exits when d is day 1 of the next month
48.
49. // print final end of line if necessary
50. if (weekday != Calendar.SUNDAY)
51. System.out.println();
52. }
53.}
为什么要考虑加粗那些呢?

请看过这本书的朋友帮忙解决下这个问题。谢谢。

2.Re:《JAVA2 核心技术 卷I》一个程序询问? [Re: 浦饭] Copy to clipboard
Posted by: welcomeriver
Posted on: 2005-03-17 15:15

为了对其,从1到9是一位数,加一个空格是为了与后面的两位数的day对齐

3.Re:《JAVA2 核心技术 卷I》一个程序询问? [Re: 浦饭] Copy to clipboard
Posted by: 浦饭
Posted on: 2005-03-17 19:15

非常感谢.

不过我自己测试的时候发现日期仍然没有对齐,考虑也许是星期表示占用了三位,日期最多的是两位,好象是他程序本身的问题.

18. // print heading
19. System.out.println("Sun Mon Tue Wed Thu Fri Sat");


晚上自习后再修改试下.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923