java一个日历程序问题?

haoxiongok 2008-02-25 03:17:59
各位大虾给看看这个程序:
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);

[b] System.out.printf("%3d",day);[/b]

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();
}

}
有颜色那一行总是报错,大家帮忙看看是什么错阿。。。
...全文
124 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
abang8114 2008-02-25
  • 打赏
  • 举报
回复
用12楼的方法倒是能运行起来,只可惜日期全变为NULL了
logi22 2008-02-25
  • 打赏
  • 举报
回复
printf(String, Object[])对于参数(String, Integer)不适用
错在少了[],把Integer转成Integer数组就可以了

Integer[] objDay = new Integer(day);
然后把objDay传入,即
System.out.printf("%3d",objDay);
abang8114 2008-02-25
  • 打赏
  • 举报
回复
我运行了一下和LZ出现一样的毛病我用的也是eclipse.
10楼的说转Integer可还是报错类型 PrintStream 中的方法 printf(String, Object[])对于参数(String, Integer)不适用
  • 打赏
  • 举报
回复
呵呵,楼主的这个错说了,“类型PrintStream 中的方法 printf(String, Object[])对于自变量(String, int)不适用 CalendarTest.java” 稍稍观察,发现参数类型不匹配,原方法是需要Object对象(这在JDK文档中也可以查到),而楼主用了int,但int是基本数据类型,应将其转为对象,
Integer objDay = new Integer(day);
然后把objDay传入,即
System.out.printf("%3d",objDay);
但我的JRE是1.5的,没有抱错,以上仅供参考,望对你有助~~
老紫竹 2008-02-25
  • 打赏
  • 举报
回复
看2楼!
haoxiongok 2008-02-25
  • 打赏
  • 举报
回复
老紫竹,你能说明白点吗?
我新手, 不太懂
haoxiongok 2008-02-25
  • 打赏
  • 举报
回复
天啊,我无语了,我也是1.6版本的阿,怎么就不行呢?
气死我了
老紫竹 2008-02-25
  • 打赏
  • 举报
回复
for (int i = Calendar.SUNDAY; i < weekday; i++)
System.out.print(" ");

你少写了一半的空格,因为SUN也占三个哦!
Kreocn 2008-02-25
  • 打赏
  • 举报
回复
不知道你是什么JRE版本(我是1.6)....反正我是运行正常...所以我来接分了...哈哈

运行结果:

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
haoxiongok 2008-02-25
  • 打赏
  • 举报
回复
怪了嘿,怎么在我这就报错呢。。
难道eclipse耍我??
老紫竹 2008-02-25
  • 打赏
  • 举报
回复
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Test {
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.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();
}
}


运行正常
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

老紫竹 2008-02-25
  • 打赏
  • 举报
回复
啥错? 贴出来。光看那一句,没发现什么问题!
haoxiongok 2008-02-25
  • 打赏
  • 举报
回复
类型 PrintStream 中的方法 printf(String, Object[])对于自变量(String, int)不适用 CalendarTest.java
总是报这个错,看不懂,请指教

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧