spring BookStore程序中出现多个错误[已关闭]

gjmwrych  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(90)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
Improve this question
有一个问题的程序和我得到多个错误
第一个
在打破case 2和字符串bt和公共类布尔值myBookStore.inStock.之前,我如何解决这个问题?使用即兴语法的建议会更有帮助。

import java.util.Scanner;
import java.util.ArrayList;
public class MyBookstore {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Bookstore myBkStore = new Bookstore();

    int y = 0;
    int user_choice = 2;
    boolean quit = false;

    do {
        //display menu to user
        //ask user for his choice and validate it (make sure it is between 1 and 6)
        System.out.println();
        System.out.println("1) Add a book to the stock");
        System.out.println("2) Sell a book in stock");
        System.out.println("3) List the titles of all the books in stock (in the Bookstore object)");
        System.out.println("4) List all the information about the books in stock (in the Bookstore object)");
        System.out.println("5) Print out the gross income of the bookstore");
        System.out.println("6) Quit");
        System.out.println();
        System.out.print("Enter choice [1-6]: ");
        user_choice = s.nextInt();
        switch (user_choice) {
            case 1: System.out.println("Enter a book title");

                    String bt = s.next();           //stores title of book user enters in

                    if (myBkStore.inStock(title, y))
                    {
                        System.out.println("How many more to add to the stock");
                        y = s.nextInt();
                        myBkStore.addBookQuantity(bt, y);
                    }
                    else
                    {
                        System.out.println("Enter the amount of pages of the book: ");
                        int pages = s.nextInt();
                        System.out.println("Enter the price of the book: ");
                        double price = s.nextDouble();
                        System.out.println("Enter the quantity to add: ");
                        int quant = s.nextInt();
                        //myBkStore.Book(bt, pages, price, quant);
                        myBkStore.addNewBook(book);
                    } 

                    break;
            case 2: System.out.println("Enter book title to buy: ");
                    String bookT = s.next();
                    myBkStore.sellBook(title, y);
                    break;
            case 3: myBkStore.listTitles();
                    break;
            case 4: myBkStore.listBooks();
                    break;
            case 5: myBkStore.getIncome();
                    break;
            case 6: System.out.println("Thanks for coming");
                    quit = true;
                    break;
            default: System.out.println("\nInvalid Choice");
        }
}
while (!quit);
}

static class Bookstore {
private Book[] books; // all the books in this bookstore
private int totalBooks; // the number of books in this bookstore
    private double grossIncome; //the gross income of the bookstore (will be incremented when books are sold)

// Constructor: A new Bank object initially doesn’t contain any accounts.
public Bookstore() {
    books = new Book[100];
    totalBooks = 0;
    grossIncome = 0;
    }

// Creates a new bank account using the customer name and the opening balance given as parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public void addNewBook(Book b) {
    if(totalBooks < books.length) {
        books[totalBooks] = b;
        totalBooks++;

    }
    else
    {
        System.out.println("\nSorry, cannot add book to stock.");
    }

}

public void addBookQuantity(String title, int quantity) {
    for (int i =0; i<totalBooks; i++) {
        if (title == books[i].getTitle()  ) {
            books[i].addQuantity(quantity);
            System.out.println("Quantity added successfully");
            return;
        }
    }

    /*int i;

    for (i = 0; i < totalbooks; i++)
    {
        if((books[i].getTitle()).equals(title))
        {
            books[i].addQuantity(quantity);
            return;
        }
    }*/

}

public boolean inStock (String title, int quantity) {
    for (int i =0; i<totalBooks; i++) {
            if (title.equals(books[i].getTitle())) {
                if (quantity <= books[i].getQuantity()) {return true;}
                else {return false;}
            }
        }
    return false;

}

public boolean sellBook(String title, int quantity){
    int i;

        boolean sellflag=false;

        // Checks to see if the books are in stock.

        boolean retval = inStock(title, quantity);

        // If so, completes the sale.

        if (retval) {

          for (i=0; i<totalBooks && !sellflag; i++) {

            if (title.equals(books[i].getTitle())) {

              books[i].subtractQuantity(quantity);

              grossIncome += (books[i].getPrice()) * quantity;

              sellflag = true;

            }

          } // for

        } // if

        return retval;

      } // sellBook

public void listTitles()
{
    for (int i = 0; i < totalBooks; i++)
    {
        System.out.println(books[i].getTitle());
    }

}

public void listBooks()
{
    int i;

    System.out.println("\nList of Books\n======");
    for (i = 0; i < totalBooks; i++)
    {
        System.out.println(books[i]);
    }
    System.out.println();
}

public double getIncome()
{
    return grossIncome;
}

}

  static class Book{

       private String title;
       private int numOfPages;
       private double price;
       private int quantity;
       private Book book;

       public String toString(){
           return "Title: " + title + "\nNumber of pages: " + numOfPages + "\nPrice:" + price +"\nQuantity: " + quantity + "\n";

       }

       public Book book(String thetitle, int pages, double cost, int num){
         /*title = thetitle;
         numOfPages = pages;
         price = cost;
         quantity = num;*/
         return book;

       }

       public String getTitle(){
         return title;
       }

       public double getPrice(){
         return price;
       }

       public int getQuantity(){
         return quantity;
       }

       public void addQuantity(int amount){
            quantity = quantity + amount;

    }
    public void subtractQuantity(int amount)
    {
          System.out.println("Amount to buy");
        Scanner s = new Scanner(System.in);
        quantity = s.nextInt();
        quantity = quantity - amount;   
    }

}//end of class
}
5t7ly7z5

5t7ly7z51#

我觉得很慷慨,所以我修正了你的代码。所有的编译错误都消失了,还修正了一些逻辑错误。

public class MyBookstore {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Bookstore myBkStore = new Bookstore();

        int y = 0;
        int user_choice = 2;
        boolean quit = false;

        do {
            // display menu to user
            // ask user for his choice and validate it (make sure it is between 1 and 6)
            System.out.println();
            System.out.println("1) Add a book to the stock");
            System.out.println("2) Sell a book in stock");
            System.out.println("3) List the titles of all the books in stock (in the Bookstore object)");
            System.out.println("4) List all the information about the books in stock (in the Bookstore object)");
            System.out.println("5) Print out the gross income of the bookstore");
            System.out.println("6) Quit");
            System.out.println();
            System.out.print("Enter choice [1-6]: ");
            user_choice = s.nextInt();
            s.nextLine(); // THIS IS NEEDED TO MOVE THE SCANNER TO THE NEXT LINE BEFORE GETTING THE TITLE.
            switch (user_choice) {
            case 1:
                System.out.print("Enter a book title: ");
                String bt = s.nextLine(); // stores title of book user enters in
                if (myBkStore.inStock(bt)) {
                    System.out.print("How many more to add to the stock: ");
                    y = s.nextInt();
                    myBkStore.addBookQuantity(bt, y);
                } else {
                    System.out.println("Enter the amount of pages of the book: ");
                    int pages = s.nextInt();
                    System.out.println("Enter the price of the book: ");
                    double price = s.nextDouble();
                    System.out.println("Enter the quantity to add: ");
                    int quant = s.nextInt();
                    // myBkStore.Book(bt, pages, price, quant);
                    myBkStore.addNewBook(new Book(bt, pages, price, quant));
                }

                break;
            case 2:
                System.out.println("Enter book title to buy: ");
                String bookT = s.nextLine();
                myBkStore.sellBook(bookT);
                break;
            case 3:
                myBkStore.listTitles();
                break;
            case 4:
                myBkStore.listBooks();
                break;
            case 5:
                System.out.println("\nStore Gross income: " +  myBkStore.getIncome());
                break;
            case 6:
                System.out.println("Thanks for coming");
                quit = true;
                break;
            default:
                System.out.println("\nInvalid Choice");
            }
        } while (!quit);
        s.close();
    }

    static class Bookstore {
        private Book[] books; // all the books in this bookstore
        private int totalBooks; // the number of books in this bookstore
        private double grossIncome; // the gross income of the bookstore (will be incremented when books are sold)

        // Constructor: A new Bank object initially doesn’t contain any accounts.
        public Bookstore() {
            books = new Book[100];
            totalBooks = 0;
            grossIncome = 0;
        }

        // Creates a new bank account using the customer name and the opening balance
        // given as parameters
        // and returns the account number of this new account. It also adds this account
        // into the account list
        // of the Bank calling object.
        public void addNewBook(Book b) {
            if (totalBooks < books.length) {
                books[totalBooks] = b;
                totalBooks++;
            } else {
                System.out.println("\nSorry, cannot add book to stock.");
            }
        }

        public void addBookQuantity(String title, int quantity) {
            for (int i = 0; i < totalBooks; i++) {
                if (title == books[i].getTitle()) {
                    books[i].addQuantity(quantity);
                    System.out.println("Quantity added successfully");
                    return;
                }
            }
        }

        public boolean inStock(String title) {
            for (int i = 0; i < totalBooks; i++) {
                if (title.equals(books[i].getTitle())) {
                    if ( books[i].getQuantity() > 0) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            return false;
        }

        public boolean sellBook(String title) {
            int i;
            boolean sellflag = false;

            // Checks to see if the books are in stock.
            boolean retval = inStock(title);

            // If so, completes the sale.
            if (retval) {
                for (i = 0; i < totalBooks && !sellflag; i++) {
                    if (title.equals(books[i].getTitle())) {
                        int quantity = books[i].subtractQuantity();
                        grossIncome += (books[i].getPrice()) * quantity;
                        sellflag = true;
                    }
                }
            }
            return retval;
        }

        public void listTitles() {
            for (int i = 0; i < totalBooks; i++) {
                System.out.println(books[i].getTitle());
            }
        }

        public void listBooks() {
            int i;

            System.out.println("\nList of Books\n======");
            for (i = 0; i < totalBooks; i++) {
                System.out.println(books[i]);
            }
            System.out.println();
        }

        public double getIncome() {
            return grossIncome;
        }
    }

    private static class Book {

        private String title;
        private int numOfPages;
        private double price;
        private int quantity;

        public String toString() {
            return "Title: " + title + "\nNumber of pages: " + numOfPages + "\nPrice:" + price + "\nQuantity: "
                    + quantity + "\n";
        }

        public Book(String thetitle, int pages, double cost, int num) {
            title = thetitle;
            numOfPages = pages;
            price = cost;
            quantity = num;
        }

        public String getTitle() {
            return title;
        }

        public double getPrice() {
            return price;
        }

        public int getQuantity() {
            return quantity;
        }

        public void addQuantity(int amount) {
            quantity = quantity + amount;

        }

        public int subtractQuantity() {
            System.out.println("Amount to buy");
            Scanner s = new Scanner(System.in);
            int amount = s.nextInt();
            quantity = quantity - amount;
            return amount;
        }
    }
}

示例输出:

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 1
Enter a book title: Effective Java
Enter the amount of pages of the book: 
300
Enter the price of the book: 
29.99
Enter the quantity to add: 
10

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 1
Enter a book title: Learn Python
Enter the amount of pages of the book: 
100
Enter the price of the book: 
10.00
Enter the quantity to add: 
5

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 2
Enter book title to buy: 
Effective Java
Amount to buy
1

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 3
Effective Java
Learn Python

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 4

List of Books
======
Title: Effective Java
Number of pages: 300
Price:29.99
Quantity: 9

Title: Learn Python
Number of pages: 100
Price:10.0
Quantity: 5

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 5

Store Gross income: 29.99

1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4) List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit

Enter choice [1-6]: 6
Thanks for coming

您的主要问题与Scanner的使用有关。
1.对于标题,因为它们可以包含多个单词(用空格分隔),所以需要使用Scanner#nextLine()而不是Scanner#next()
1.当你在主菜单中输入数字时,你需要占用这一行。调用Scanner#nextInt()并不会占用这一行。因此,你需要在下一个提示之前调用Scanner#nextLine(),这样扫描程序就会移动到下一行,以获取下一个输入的值。

相关问题