java - 调用内部带有 for 循环的方法,而不更新变量

标签 java loops class for-loop methods

我正在为计算机科学课上的实验室创建一个书店。它使用三个相互交互的类来创建一个书店,用于添加书籍、销售书籍、展示图书馆等。我正在使用对象数组。当我向图书馆添加多本书,然后尝试访问它们以出售它们时,我的问题就出现了。我认为问题在于“inStock”方法,我用它来确定我们是否有特定的书籍要销售。我不确定如何访问我添加的所有书籍来销售它们,并且我不确定我的方法是否是执行此操作的最佳方式。

每当我尝试出售不是我添加的第一本书的书籍时,程序都会声称他们没有该书的库存,即使我可以使用不同的方法显示所有书籍。

如何使用此方法来检测我使用 inStock 方法添加的所有列出的书籍?

        // Search for the book...if found, adjust the quantity.      
        // otherwise, Book not in the BookStore.
        for(int i = 0; i < totalbooks; i++) {
            if(title.equals(books[i].getTitle())) {
                if(quantity <= books[i].getQuantity()) {
                    return true;

                }
                else
                    return false;
            }
            else return false;

        }
        return false;
    }

//this is the inStock method^
public boolean sellBook(String title, int quantity) {
        // Checks to see if the books are in stock.
        for(int i = 0; i < totalbooks; i++) {
            if(title.equals(books[i].getTitle())) {
                if(quantity <= books[i].getQuantity()) {
                    gross = gross + books[i].getQuantity()*books[i].getPrice();
                    books[i].subtractQuantity(quantity);

                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
        return false;
    }
//this is the method I use to sell the books

case 2: System.out.println("Please enter the book title.");
                title = input.next();
                System.out.println();

                //input.hasNext();
                if(!(bookstore.inStock(title, quant))) {
                    System.out.println("I'm sorry this book is not in stock.");
                }
                else {
                    System.out.println("How many copies would you like to buy?");
                    quant = input.nextInt();
                    if(bookstore.inStock(title, quant)) {
                    bookstore.sellBook(title, quant);
                    System.out.println("You just bought " + quant +" copies of " + title);
                    }
                    else
                        System.out.println("Error: Not enough copies in stock for your purchase."); break;
//this is a part of the demo class that I use to try to sell the book.

最佳答案

问题确实出在您的 inStock 方法中 - return 语句立即停止方法的执行。除了第一本书之外,没有机会检查其他书籍的可用性。事实上,您只需要两个 return 语句:

for (int i = 0; i < totalbooks; i++) {
    if(title.equals(books[i].getTitle())) {
        return quantity <= books[i].getQuantity();
    }
}
return false;

关于java - 调用内部带有 for 循环的方法,而不更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532372/

相关文章:

java - 小于或等于 GregorianCalendar

java - 如何在 libgdx 中正确实现声音切换?

java - 如何加快查找 id 的循环速度

Java .. 给定公共(public)类并且没有 Main 方法作为小组项目的一部分.. 我该如何编译?

python-3.x - 有属性但没有方法的类

c++ - 如何将类对象推回到 std::vector 中?

Java 类型转换列表

java构造函数不能应用于给定类型android studio

javascript - 动态按钮 ID - Javascript