如何使用使用 JOptionPane 的构造函数将对象从我的方法传递到空数组中



我在弄清楚如何存储构造函数中的对象时遇到了麻烦。 到目前为止,我得到的只是一个对象,其余的都是空的。如果有人可以向我解释,以便初学者能够理解,将不胜感激。

public class Bookstore 
{
    /*
    Main Class Bookstore to be made modular 
     */
    public static void main(String args[]) 
    {
        Book catalogue[] = new Book[3];
        int select;
        do
        {
            select = bookMenu();
            switch(select)
            {
            case 1:  
                int i =0;
                if(catalogue[i] != null)
                {
                    JOptionPane.showMessageDialog(null,"Test");
                    break;
                }
                catalogue[i] = addBook();
            case 2:     
                sortBook();     
            break;
            case 3:     
                searchBook(catalogue);   
            break;
            case 4:     
                displayBook(catalogue);  
            break;
            case 5:
            break;
            }   
        }
        while(select != 5);
    }
    public static int bookMenu()
    {
        int select;
        String menuOptions = "--Book store--n"
                + "n1. Add book to catalogue"
                + "n2.Sort and display books by price"
                + "n3. Search for a book by title" 
                + "n4. Display all books"
                + "nn5. Exit";
        do
        {
            select = Integer.parseInt(JOptionPane.showInputDialog(menuOptions)); 
        }
        while(select < 1 || select > 5);
        return select;  
    }
    public static Book addBook()
    {
        int isbn;
        String title, author;
        Book catalogue = null;
        double price;
        for(int i=0; i<3;i++)
        {   
            isbn = Integer.parseInt(JOptionPane.showInputDialog
                                ("Enter Book ISBN or: "));
            title = JOptionPane.showInputDialog
                                ("Enter Book Title: ");
            author = JOptionPane.showInputDialog
                                ("Enter Book Author: ");
            price = Double.parseDouble(JOptionPane.showInputDialog
                                ("Enter Book Price: "));    
            catalogue = new Book(isbn, title, author, price);    
        }
        return catalogue;
    }
    public static void sortBook()
    {
    }
    public static void searchBook(Book catalogue[])//remain void
    {
        String searchValue = JOptionPane.showInputDialog("Enter the title of the book you are searching for");
        boolean found = true;
        for(int i=0; i<catalogue.length && catalogue[i] != null ; i++)          
        {
            if(searchValue.equalsIgnoreCase(catalogue[i].getTitle()))  
            {
                JOptionPane.showMessageDialog(null, "Book details: " + catalogue[i].toString()); 
                found = true; 
            }  
        }
        if(found == false)
        JOptionPane.showMessageDialog(null, "The title does not exist in the collection ");
    }
    public static void displayBook(Book catalogue[])//remain void
    {
        String output = "";
        for(Book bk:catalogue)
        {
        output += bk + "n";
        }        
        JOptionPane.showMessageDialog(null, output);
    }
}

所以,这总是错误的...

if(count == max)

您可以在该语句之前设置值。零永远不会等于十。

一些 IDE 甚至会向您指出这一点。


如果要获取可在方法之间使用的变量,则需要学习变量作用域。

例如,使用静态类变量

private static Book[] books;
private static final int  MAX_BOOKS = 10;
private static int count;
public static void main(String[] args) {
    books = getBooks();
    bookMenu();
}
public static Book[] getBooks() {
    if(count == MAX_BOOKS) {
        JOptionPane.showMessageDialog(null, "Catalogue full - cannot add any more books");
    } else {
        for(; count < MAX_BOOKS; count++) {
            books[count]= addBook();
        }
    } 
    return books;
}

然后,如果要重复菜单,请使用循环。不要把它放在每个方法的末尾。

此外,searchBook(Book)不是在搜索标题。您希望将字符串传递给方法,而不是 Book 类

最新更新