【新人】java中printf错误问题

zt_tang 2016-03-15 10:10:12
为什么我的printf一直报错?用的是参考书的源代码,还是不行。
import java.text.DateFormatSymbols;
import java.util.*;

public class CalendarTest
{
public static void main(String[] args)
{
// construct d as current date
GregorianCalendar d = new GregorianCalendar();

int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);

// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);

int weekday = d.get(Calendar.DAY_OF_WEEK);

// get first day of week (Sunday in the U.S.)
int firstDayOfWeek = d.getFirstDayOfWeek();

// determine the required indentation for the first line
int indent = 0;
while (weekday != firstDayOfWeek)
{
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}

// print weekday names
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
do
{


System.out.printf("%4s", weekdayNames[weekday]);
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)


d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (weekday != firstDayOfWeek);
System.out.println();

for (int i = 1; i <= indent; i++)
System.out.print(" ");

d.set(Calendar.DAY_OF_MONTH, 1);
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);

// mark current day with *
if (day == today) System.out.print("*");
else System.out.print(" ");

// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);

// start a new line at the start of the week
if (weekday == firstDayOfWeek) System.out.println();
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month

// print final end of line if necessary
if (weekday != firstDayOfWeek) System.out.println();
}
...全文
366 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
家里敷泥呀 2016-03-16
  • 打赏
  • 举报
回复
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String) 这是错误信息? 这分明说的是,第二个参数必须是Object[]不支持String。 实际上我用的jkd1.7的 printf 方法签名是 printf(Stringformat, Object... args) // 1.5以后提供的可变长参数。 你的jdk是哪个版本? 如果不考虑版本问题,你也可以把String封装到Object[]里面去试试。

58,454

社区成员

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

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