可能重复表格中的Java程序:
Need help creating a java program that prints a table of contents from a book创建输出的内容
我应该创建一个程序输出表的内容。让我给你们介绍一下我的任务。用户应该在循环中输入章节标题和起始页码,直到用户为章节标题输入4个星号。用户输入4个星号后,我应该打印输出,如
国王亚瑟法院....................... ...... 3
骑士圆桌的...... ............ 8
Dinadan先生的幽默大师.......... ........ 12
我想我的大部分编码权。在我的测试程序中,我创建了一个while循环,允许用户输入章节标题和起始页码。当用户为章节标题输入4个星号时,此循环将终止。但是,我卡住的地方是在我的while循环之后,在用户输入星号之后生成输出。而不是显示目录,我得到一个输出。我尝试了一个for循环来打印标题,点号和页码,但它只显示星号。我认为循环是错误的。
这里是我到目前为止的代码:
// My Table of Contents class
public class TocEntry
{
public TocEntry(String title, int page) // 2 Argument Constructor
{
chapterTitle = title;
startPage = page;
}
public String toString() // toString method to print out chapter title,
// given dots, and start page
{
String temp = "" + startPage;
int numDots = 60 - (temp.length()+ chapterTitle.length());
String toc_out = chapterTitle;
for(int i = 0; i < numDots; i++)
toc_out=toc_out +".";
toc_out= toc_out + startPage;
return toc_out;
}
private String chapterTitle;
private int startPage;
}// End of class TocEntry
// My Test program
import java.util.Scanner;
public class useToEntry {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
final int TOCSIZE = 100;// Max size of TOC
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
System.out.print("Enter chapter title: ");
String chapterTitle = input.next();
while(!"****".equals(chapterTitle))
{
System.out.print("Enter starting page number: ");
int page = input.nextInt();
System.out.print("Enter chapter title: ");
chapterTitle= input.next();
toc [toc_curlen] = new TocEntry(chapterTitle,page);
toc_curlen = toc_curlen++;
}// End of while loop
for (int line = 0; line < toc_curlen; line++)
{
System.out.println(chapterTitle.toString());
}// End of for loop
}// End of main method
}// End of class useToEntry
任何人都可以请帮我纠正我的循环产生我的输出。我接受建议。
谢谢!
我们不是已经做到这一点? http://stackoverflow.com/questions/12455148/need-help-creating-a-java-program-that-prints-a-table-of-contents-from-a-book/12456026#12456026 – MadProgrammer
因此投票关闭作为重复? –
@Marko [标签:家庭作业]标签已经死了。不再使用它。 –