创建一个输出目录的Java程序 [英] Creating a Java Program that outputs a table of contents

查看:95
本文介绍了创建一个输出目录的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
需要帮助创建一个从书中打印出目录的Java程序

Possible Duplicate:
Need help creating a java program that prints a table of contents from a book

我应该创建一个输出目录的程序.让我给大家介绍我的工作背景.用户应该循环输入章节标题和起始页码,直到用户输入4个星号作为章节标题.用户输入4个星号后,我应该打印输出,例如

I'm supposed to create a program that outputs a table of contents. Let me give you guys a background of my assignment. The user is supposed to enter a chapter title and a starting page number in a loop, until the user enters 4 asterisks for the chapter title. After the user enters the 4 asterisks, I'm supposed to print out the output, something like

亚瑟王法院...................................... 3

King Arthur's Court.............................3

桌上骑士团... ............ 8

Knights of the Table Round... ............8

幽默大师狄娜丹爵士......... 12

Sir Dinadan the Humorist..................12

我认为我拥有大部分的编码权利.在测试程序中,我创建了一个while循环,允许用户输入章节标题和起始页码.当用户为章节标题输入4个星号时,此循环将终止.但是,我遇到的麻烦是在while循环之后,在用户输入星号后生成输出.我没有显示目录,而是得到了输出.我尝试了for循环以打印出标题,点和页码,但它仅显示星号.我认为循环是错误的.

I think I have most of my coding right. In my test program, I created a while loop that allows the user to enter a chapter title and a starting page number. This loop will terminate when the user enters 4 asterisks for the chapter title. But, where I'm stuck is after my while loop, to generate the output after the user enters the asterisks. Instead of having the table of contents displayed, I'm getting an output. I tried a for loop to print out the title, dots, and page number, but it only displays asterisks. I think that the loop is wrong.

到目前为止,这是我的代码:

Here's my code so far:

// 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

任何人都可以帮助我纠正循环以生成输出的问题.我愿意提出建议.

Can anyone please help me correct my loop to generate my output. I'm open to suggestions.

谢谢!

推荐答案

一些建议:

toc_curlen = toc_curlen++;

将导致toc_curlen永不递增.您可以单独使用增量运算符:

will cause toc_curlen never to increment. You can just use the increment operator on its own:

toc_curlen++;

您第一次使用

String chapterTitle = input.next();

您永远不会将其分配给您的TocEntry数组toc,因此本章将永远不会显示.

you never assign it to your TocEntry array toc, so this chapter will never be shown.

您正在显示循环中重复打印变量chapterTitle-您应该在此处打印TocEntry:

You are printing the variable chapterTitle repeatedly in the display loop - you should print your TocEntry here:

for (int line = 0; line < toc_curlen; line++) {
   System.out.println(toc[line].toString());
}

这篇关于创建一个输出目录的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆