简体   繁体   English

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

[英]Calling a method with a for loop inside, not updating variables

I am creating a bookstore for a lab in my computer science class.我正在为我的计算机科学 class 的实验室创建一个书店。 It uses three classes that interact with each other to create a bookstore that adds books, sells books, displays the library, and so on.它使用三个相互交互的类来创建一个书店,添加书籍、销售书籍、展示图书馆等等。 I am using an array of objects.我正在使用一组对象。 My issue comes when I'm adding multiple books to the library, then I try to access them to sell them.当我向图书馆添加多本书时,我的问题就出现了,然后我尝试访问它们以出售它们。 I think the issue lies with the "inStock" method that I use to determine if we have a specific book to sell.我认为问题在于我用来确定我们是否有要出售的特定书籍的“inStock”方法。 I'm not sure how to access all the books that I added to sell them, and I'm not sure if my method is the best way to do this.我不确定如何访问我添加的所有书籍以出售它们,我不确定我的方法是否是最好的方法。

Whenever I try to sell a book that isn't the first one that I added, the program claims that they do not have that book in stock, even though I can display all the books with a different method.每当我尝试出售不是我添加的第一本书时,程序声称他们没有这本书的库存,即使我可以用不同的方法显示所有的书。

How could I get this method to detect all the listed books that I added with the inStock method?我怎样才能让这种方法检测我使用 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.

Check you conditions:检查你的条件:

if(title.equals(books[i].getTitle())) {
    if(quantity <= books[i].getQuantity()) {
       return true;

    }
    else
       return false;
}

else return false; // <-- exits method after first instance of non-matching titles

It's typically bad practice to have multiple return statements in a method for reasons like this.出于这样的原因,在一个方法中包含多个 return 语句通常是不好的做法。

The issue is indeed in your inStock method - return statement immediately stops the execution of a method.问题确实出在您的 inStock 方法中 - return语句立即停止方法的执行。 There is no chance for other books apart from the first one to be checked for availability.除了第一本书之外,没有机会检查其他书籍的可用性。 In fact, you only need two return statements:其实只需要两条return语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM