本文介绍了我输入数组保持在写itsself的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我传递一个对象来我的数组,它将覆盖previous条目。任何人都可以找出为什么发生这种情况?

addbook() - 当我把名字和作者它分配一个值,但是当进入另一个标题和作者,它会覆盖previous条目

 公共类库{
    静态扫描键盘=新的扫描仪(System.in);
    静态诠释计数= 0;
    公共静态无效的主要(字串[] args){
         addBook();
    } //主结束
    静态无效addBook(){
        loanbook [] loanArray =新loanbook [5];        串标题,作者;
        INT选择;
        布尔onLoan;
        loanbook book1的; //测试之用
        为(中间体X = 0; X小于5; X ++){
            System.out.print(为小说或2非小说preSS 1:); //为小说和非小说子菜单
            选择= keyboard.nextInt();
            如果(选择== 1){                的System.out.println(请输入书名:);
                标题= keyboard.nextLine();
                标题= keyboard.nextLine();
                的System.out.println(请输入书的作者:);
                笔者= keyboard.nextLine();
                onLoan = FALSE; //尚未使用
                BOOK1 =新小说(标题,作者);
                的System.out.println(book1.toString());
                loanArray [X] =新loanbook(标题,作者);            }
            否则,如果(选择== 2){
                的System.out.println(请输入书名:);
                标题= keyboard.nextLine();
                标题= keyboard.nextLine();
                的System.out.println(请输入书的作者:);
                笔者= keyboard.nextLine();
                onLoan = FALSE; //尚未使用
                BOOK1 =新纪实(标题,作者);
                的System.out.println(book1.toString());
                loanArray [X] =新loanbook(标题,作者);
            }
        }
    }
} //结束图书馆

我Loanbook类

 公共类loanbook {
    私人字符串标题,作者;
    私人诠释BOOKID,计数= 0;    公共loanbook(字符串pTitle,字符串pAuthor){
        BOOKID =计数;
        标题= pTitle;
        笔者= pAuthor;
        算上++;
    } //构造函数
    公共无效的setTitle(字符串pTitle){
        标题= pTitle;
    } //的setTitle
    保护串的getTitle(){
        返回称号;
    } //的getTitle
    保护字符串getAuthor(){
        返回作者;
    } // getAuthor
    公共字符串的toString(){
        返回的BookID:+ BOOKID +\\ n+标题:+的getTitle()+\\ n+作者:+ getAuthor()+\\ n;
    }
} // loanbook


解决方案

您可能希望计数 静态 。我假设你想要的计数每次创建新的预订时间上去。如果没有静态,计数值不会永远持续创建一本书的时间,让你的 BOOKID 将始终为0的每一本书。这的可能的是,为什么你认为的它变得自动覆盖的。我不完全确定,因为你还没有真正解释是什么意思。

 私人诠释BOOKID;
公共静态诠释计数= 0; < - 静公共loanbook(字符串pTitle,字符串pAuthor){
    BOOKID =计数;
    标题= pTitle;
    笔者= pAuthor;
    算上++;
}

或者更好的,只是避免了计数变量。你做同样的,你会使用计数。因此,计数是不必要的。

 公共静态INT BOOKID 0;公共loanbook(字符串pTitle,字符串pAuthor){
    标题= pTitle;
    笔者= pAuthor;
    BOOKID ++;
}

另外,我不知道你打算用 loanbook BOOK1什么; ,但它用在循环每一次,所以我可以看到这是可能的它覆盖掉了的问题。假设小说纪实延长 loanbook

此外,我不认为有必要为计数库中的类。你可以摆脱这一点。

更新:

假设你想要的小说纪实书(他们都扩展 loanbook)在 loanArray`可能你想这样的事情

  loanbook书=新小说(标题,作者);
的System.out.println(book1.toString());
loanArray [X] =书;

Every time i pass an object to my array, it overwrites the previous entry. Can anybody spot why this is happening?

addbook() - when i put in name, and author it assigns a value , but when enter another title and author, it overwrites the previous entry.

public class library {
    static Scanner keyboard = new Scanner(System.in);
    static int count = 0;
    public static void main(String [] args){
         addBook();
    }   // Main end
    static void addBook(){
        loanbook [] loanArray = new loanbook[5];

        String title,author;
        int choice;
        boolean onLoan;
        loanbook book1; // TESTING ONLY
        for(int x = 0; x < 5; x++){
            System.out.print("Press 1 for Fiction or 2 for Non Fiction: ");  // sub menu for fiction and non fiction
            choice = keyboard.nextInt();
            if (choice == 1){

                System.out.println("Please enter book title: ");
                title = keyboard.nextLine();
                title = keyboard.nextLine();
                System.out.println("Please enter book author: ");
                author = keyboard.nextLine();
                onLoan = false; // not used yet
                book1 = new fiction(title,author);
                System.out.println(book1.toString());
                loanArray[x] = new loanbook(title,author);

            }
            else if (choice == 2) {
                System.out.println("Please enter book title: ");
                title = keyboard.nextLine();
                title = keyboard.nextLine();
                System.out.println("Please enter book author: ");
                author = keyboard.nextLine();
                onLoan = false; // not used yet
                book1 = new nonfiction(title,author);
                System.out.println(book1.toString());
                loanArray[x] = new loanbook(title,author);
            }
        }
    }
} // Library end

My Loanbook class

public class loanbook {
    private String title,author;
    private int bookID, count = 0;

    public loanbook(String pTitle,String pAuthor){
        bookID = count;
        title = pTitle;
        author = pAuthor;
        count++;
    }  // Constructor
    public void setTitle(String pTitle){
        title = pTitle;
    } // setTitle
    protected String getTitle(){
        return title;
    }   // getTitle
    protected String getAuthor(){
        return author;
    }   // getAuthor
    public String toString(){
        return " BookID: "+ bookID+"\n" + " Title: "+ getTitle()+"\n" +" Author : "+ getAuthor()+ "\n";
    }
}  // loanbook
解决方案

You may want to make count static. I'm assuming you want the count to go up every time a new book is created. Without the static, the count value will not persist ever time a book is created, so your bookID will always be 0 for every book. That may be why you think "it's getting overwriten". I'm not totally sure because you haven't really explained what that means.

private int bookID;
public static int count = 0;           <-- static

public loanbook(String pTitle,String pAuthor){
    bookID = count;
    title = pTitle;
    author = pAuthor;
    count++;
}

Or better yet, just avoid the count variable. You do the same as you would with the count. So count is unnecessary.

public static int bookID 0;

public loanbook(String pTitle,String pAuthor){
    title = pTitle;
    author = pAuthor;
    bookId++;
}

Also, I don't know what you're planning to with loanbook book1;, but it is used every time in the loop, so I could see this being the possible "it is getting overwritten" problem. Assuming fiction and nonfiction extend loanbook

Also I don't see a need for the count in the library class. You can get rid of that.

Update:

Assuming you want the fiction or nonfiction book (and they both extend loanbook) in yourloanArray` may you want something like this

loanbook book = new fiction(title,author);
System.out.println(book1.toString());
loanArray[x] = book;

这篇关于我输入数组保持在写itsself的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:34