JAVA核心技术 上的关于日历的一个程序 自己试了一下 发现有问题 求教了~~
程序是这样的:
import java.util.*;
public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
d.set(Calendar.DAY_OF_MONTH,1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int i = Calendar.SUNDAY;i < weekday;i++)
System.out.print("");
do
{
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if (day == today)
System.out.print("*");
else
System.out.println(" ");
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();
}
}
我在Eclipse上试了下 发现结果是这样的:
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还是一知半解,所以看了几遍程序还有点不明白,希望大家给看看程序,谢谢
------解决方案--------------------
Java code
if (day == today)
System.out.print("*");
else
System.out.println(" ");