更新时间:2023-11-20 16:38:34
所以。您需要知道的最大可能字符串
所有章节的长度。
So. What you need to know the maximum possible String
length of all the chapters.
这是最容易实现的监控在输入阶段输入了文字...类似于......
This is easiest achieved by monitoring the text been entered during your input stage...something like...
int maxChapterLength = 0;
for(int i = 0; i < TOC_NUM; i++) {
System.out.print("Enter chapter title: ");
String ch = input.next();
maxChapterLength = Math.max(ch.length(), maxChapterLength);
System.out.print("Enter starting page number: ");
int y = input.nextInt();
}// End of for loop
然后,您可以确定所需的填充章节名称和章节号码之间
From this, you can then determine the required padding between the chapter name and the chapter number
类似......
public static String pad(String sValue, int iMinLength) {
StringBuilder sb = new StringBuilder(iMinLength);
sb.append(sValue);
while (sb.length() < iMinLength) {
sb.append(".");
}
return sb.toString();
}
从那里你只需要将信息发送到屏幕...类似......
From there you simply need to send the info to the screen...something like...
System.out.println(pad(toc[index].getChapter(), maxChapterLength + 2) + toc[index].getPage());
现在,看了你的代码,你还有其他一些问题,但我离开了它是你要识别的。
Now, having looked at you're code, you have a bunch of other problems, but I leave it to you to identify.