我的输入数组不断重写自己 [英] my input array keeps over writing itself

查看:73
本文介绍了我的输入数组不断重写自己的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我将一个对象传递给我的数组时,它都会覆盖上一个条目.有人能发现为什么会这样吗?

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

addbook()-当我输入名称并创建作者时,它会分配一个值,但是当输入其他标题和作者时,它将覆盖上一个条目.

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

我的借贷课程

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

推荐答案

您可能想创建count static.我假设您希望每当创建一本新书时计数都增加.如果没有该静态值,则每次创建一本书时,计数值都不会持续存在,因此对于每本书,您的bookID始终为0.该 可能就是您认为它已被覆盖" 的原因.我不太确定,因为您还没有真正解释这意味着什么.

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++;
}

或者更好,只是避免使用count变量.您可以执行与count相同的操作.因此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++;
}

此外,我不知道您打算使用loanbook book1;做什么,但是它在循环中每次都使用,所以我可以看到这可能是它被覆盖"了问题.假设fictionnonfiction扩展loanbook

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

我在库类中也看不到count的需要.您可以摆脱它.

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

更新:

假设您想要fictionnonfiction书(并且它们都扩展了loanbook) in your loanArray`,也许您想要这样的东西

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;

这篇关于我的输入数组不断重写自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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