我应该编写一个java程序,它使用以下数据结构打印目录:
public class TocEntry
{
// Specify the needed methods
private String chapter;
private int page;
}
以下在我的驱动程序类中定义:
public final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; //The toc_curlen is intended to keep track of the number of chapters entered by the user and it can be used as an index into the array of TocEntry objects.
接下来,我应该在我的TocEntry类中开发必要的代码,以读取章节名称和页码,直到输入“**
”。从这里,我的输出应该如下所示:
我的故事开始....................... 1
成长........................... 35
征服世界........ 103
这应该是我使用名为useTocEntry
的驱动程序运行的示例输入章节标题:Camelot
输入起始页码:1
输入章节标题:King Arthur's Court
输入起始页码:3
输入章节标题:圆桌骑士
输入起始页码:8
输入章节标题:幽默作家迪纳丹爵士
输入起始页码:12
输入章节标题:灵感
输入起始页码:14
输入章节标题:Eclipse
输入起始页码:23
输入章节标题:克拉伦斯的后记
输入起始页码:274
输入章节标题:
**
柯莱特.......................................... 1
亚瑟王的法庭......................... 3
桌上骑士............ 8
Sir Dinadan the Humorist .............. 12
灵感................................ 14
日食................................... 23
克拉伦斯的后记............. 274
这是我到目前为止的代码:
import java.util.*;
public class TocEntry {
public TocEntry() { // Default Constructor
chapter = "";
page = 0;
}
public TocEntry(String c, int p) { // 2 Argument Constructor
chapter = c;
page = p;
}
public String getChapter() { //getChapter() and getPage() are accessor methods
return chapter;
}
public int getPage() {
return page;
}
public void setChapter(String title) { ////setChapter() and setPage() are mutator methods
chapter = title;
}
public void setPage(int numPage) {
page = numPage;
}
private String chapter;
private int page;
public String toString() { // toString method to print out contents
return chapter + "**" + page;
}
}// End of class TocEntry
这是我的司机班:
import java.util.Scanner;
public class useToEntry {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of contents you would like to display: ");
int TOC_NUM = input.nextInt();
final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; // The toc_curlen is intended to keep track of the number
// of chapters entered by the user and it can be used as an
// index into the array of TocEntry objects.
for(int i = 0; i < TOC_NUM; i++) {
System.out.print("Enter chapter title: ");
String ch = input.next();
System.out.print("Enter starting page number: ");
int y = input.nextInt();
}// End of for loop
} // End of main method
}// End of class useTocEntry
我需要帮助,尤其是创建一个方法,使**
与章节标题和页码对齐。对我的代码提供任何帮助或建议将不胜感激。
答案 0 :(得分:0)
因此。您需要知道所有章节的最大可能String
长度。
通过监控输入阶段输入的文本......最简单的方法就是......
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
然后,您可以确定章节名称和章节号
之间所需的填充像...一样的东西。
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();
}
从那里你只需要将信息发送到屏幕......就像......
System.out.println(pad(toc[index].getChapter(), maxChapterLength + 2) + toc[index].getPage());
现在,看了你的代码,你还有很多其他的问题,但我留给你确认。