3

我正在为我在技术部门做的一门课程中的练习编写应用程序。它应该实例化 5 个类的对象Book,其中包含书名、作者和页数的数据字段。我在使用for.. loop. 在第一个循环之后,它每次都会跳过一个步骤,我不知道为什么。这是我的代码

import java.util.*;
public class LibraryBook2
{
    public static void main(String[]args)
{
    String name;
    String author;
    int pages;
    Book[] novel = new Book[5];
    novel[0] = new Book();
    novel[1] = new Book();
    novel[2] = new Book();
    novel[3] = new Book();
    novel[4] = new Book();
    Scanner kb = new Scanner(System.in);
    for (int i = 0; i< novel.length;)
    {   
        System.out.println("Please Enter the books title");
        name = kb.nextLine();
        novel[i].setTitle(name);
        System.out.println("Please enter the books author");
        author = kb.nextLine();
        novel[i].setAuthor(author);
        System.out.println("Please enter the number of pages in this book");
        pages = kb.nextInt();
        novel[i].setPages(pages);
        System.out.println(""+novel[i].title);
        System.out.println(""+novel[i].author);
        System.out.println(""+novel[i].pages);
        ++i;
    }
    for (int x = 0; x<novel.length; x++)
    {
    System.out.print(""+ novel[x].title + "\n" + novel[x].author + "\n" + novel[x].pages);
    }
  }
}

在第一个for循环中,它循环一次,打印书名、作者和我输入的页数,就像它应该的那样。但第二次,它打印“请输入书名”,然后直接跳到第二次println,无需等待输入。我是对象数组的新手,一般是java,所以任何帮助表示赞赏。提前致谢。

4

3 回答 3

2

让我猜猜,您正在为页数输入类似“13<enter>”的内容,对吧?

您使用的程序错误。在书中的页数之后不要按 Enter。立即键入下一本书的标题,不要有空格或任何内容。代码读取一个整数,然后读取标题的一行。所以你不能在整数和标题之间放任何东西,因为这不是代码所期望的。

这使得该程序非常难以使用,因为在您已经输入标题后会出现输入标题的提示。这是一种非常愚蠢的程序编写方式,你不觉得吗?

一个简单的解决方法:在 之后kb.nextInt,调用kb.nextLine并将空行扔掉。

于 2012-05-16T05:10:21.730 回答
1

这一行:

name = kb.nextLine();

正在读取尽可能多的字符,直到找到一个换行符,然后也读取该字符。而这一行:

pages = kb.nextInt();

正在读取一系列数字字符,但保持换行符不变。

下一次循环时,会出现一个尚未读取的换行符。所以 kb.nextLine() 尽职尽责地读取该字符(即使它之前没有字符)并继续。

您可能想要做的是确保在下一次循环之前从输入缓冲区中删除这个额外的换行符。

于 2012-05-16T05:18:03.250 回答
0

像这样更改代码:

public static void main(String []arg){
    String name;
    String author;
    String pages;
    Book[] novel = new Book[2];
    novel[0] = new Book();
    novel[1] = new Book();
    novel[2] = new Book();
    novel[3] = new Book();
    novel[4] = new Book();
    Scanner kb = new Scanner(System.in);
    for (int i = 0; i< novel.length;)
    {   
        System.out.println("Please Enter the books title");
        name = kb.nextLine();
        novel[i].setTitle(name);
        System.out.println("Please enter the books author");
        author = kb.nextLine();
        novel[i].setAuthor(author);
        System.out.println("Please enter the number of pages in this book");
        pages = kb.nextLine();
        novel[i].setPages(Integer.parseInt(pages));
        System.out.println(""+novel[i].title);
        System.out.println(""+novel[i].author);
        System.out.println(""+novel[i].getPages());
        ++i;
    }
    for (int x = 0; x<novel.length; x++)
    {
    System.out.print(""+ novel[x].title + "\n" + novel[x].author + "\n" + novel[x].pages);
    }

将页码读取为 nextLine ,而不是整数。

于 2012-05-16T05:21:49.787 回答