我已经编写了一个类,并在我的类中创建了一个覆盖toString()函数。我试图通过适当的格式化来打印大量数据。我一直在尝试将其格式化为printf,但它无法正常工作。
public String toString() {
return String.format("CRN:" + "%30s", crnNum + "\n" + "Department:" + "%30s", deptCode + "\n" + "Course Number:"
+ "%30s", courseNum + "\n" + "Mode of Instruction:" + "%30s", instructMode + "\n", "Class Days:" + "%30s", meetingDays +
"\n" + "Class Times:" + "%30s", meetingTimesStart + "-" + meetingTimesEnd + "\n" + "Class Capacity:" + "%30s", classCapacity +
"\n" + "Class Enrollment:" + "%30s", classEnrollment + "\n" + "Instructor ID:" + "%30s", instructorID);
}
这样可以正确格式化第一行
CRN: crnNum
使用适当的间距,然后转到新行,但第二行最终为
Department: %30s
没有别的东西打印出来。我究竟做错了什么?我之前从未使用过String.format,因此我不确定语法是否完全不同而且我搞砸了,或者它是否显而易见我错过了。