You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

书店管理程序多错误排查及语法优化求助

书店管理程序错误修复与语法优化建议

核心错误修复

1. 未定义变量与对象实例化问题

  • 问题:case 1中使用未定义的title、book变量;case 2中使用未定义的title、全局变量y(无实际意义)。
  • 修复代码:
// case 1 修复后
case 1:
    System.out.println("Enter a book title");
    String bt = s.next();
    // 检查书籍是否已存在(需新增方法,见下文)
    if (myBkStore.isBookExist(bt)) {
        System.out.println("How many more to add to the stock");
        int addQuant = s.nextInt();
        myBkStore.addBookQuantity(bt, addQuant);
    } 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();
        // 实例化Book对象
        Book newBook = new Book(bt, pages, price, quant);
        myBkStore.addNewBook(newBook);
    }
    break;

// case 2 修复后
case 2:
    System.out.println("Enter book title to buy: ");
    String bookT = s.next();
    System.out.println("Enter quantity to buy: ");
    int buyQuant = s.nextInt();
    boolean sellSuccess = myBkStore.sellBook(bookT, buyQuant);
    if (sellSuccess) {
        System.out.println("Sale completed successfully");
    } else {
        System.out.println("Book not in stock or insufficient quantity");
    }
    break;

2. Book类构造器错误

  • 问题:Book类中book方法不是合法构造器,未初始化成员变量且返回空对象。
  • 修复代码:
static class Book{
    private String title;
    private int numOfPages;
    private double price;
    private int quantity;

    // 正确的构造器
    public Book(String thetitle, int pages, double cost, int num){
        title = thetitle;
        numOfPages = pages;
        price = cost;
        quantity = num;
    }

    // 其余方法保持不变,除了subtractQuantity
}

3. subtractQuantity逻辑错误

  • 问题:该方法错误地重新获取用户输入覆盖现有库存,且未做负数校验。
  • 修复代码:
public void subtractQuantity(int amount){
    if (quantity >= amount) {
        quantity -= amount;
    } else {
        System.out.println("Insufficient stock to sell");
    }
}

4. 字符串比较错误

  • 问题:addBookQuantity方法中用==比较字符串,应该用equals比较内容。
  • 修复代码:
public void addBookQuantity(String title, int quantity) {
    for (int i =0; i<totalBooks; i++) {
        if (title.equals(books[i].getTitle())) {
            books[i].addQuantity(quantity);
            System.out.println("Quantity added successfully");
            return;
        }
    }
    System.out.println("Book not found in stock");
}

5. inStock方法功能补充

  • 问题:原inStock方法仅判断指定数量是否充足,需要新增方法判断书籍是否存在。
  • 新增方法:
public boolean isBookExist(String title) {
    for (int i =0; i<totalBooks; i++) {
        if (title.equals(books[i].getTitle())) {
            return true;
        }
    }
    return false;
}

6. getIncome调用错误

  • 问题:case 5中调用方法但未打印结果。
  • 修复代码:
case 5:
    System.out.println("Gross Income: $" + myBkStore.getIncome());
    break;

语法与逻辑优化建议

  • 用ArrayList替代固定数组:Bookstore中books数组固定大小100,改为ArrayList<Book>更灵活,无需担心容量限制:
private ArrayList<Book> books;

public Bookstore() {
    books = new ArrayList<>();
    grossIncome = 0;
}

// addNewBook方法修改为
public void addNewBook(Book b) {
    books.add(b);
}
  • 输入处理优化:s.next()会截断空格,若书名含空格需用s.nextLine(),同时处理换行符残留:
// 在s.nextInt()后添加s.nextLine()清除换行
user_choice = s.nextInt();
s.nextLine(); // 清除输入后的换行符

// 获取书名时用
String bt = s.nextLine();
  • 菜单输入验证:添加逻辑确保用户输入的是1-6的整数,避免输入非数字导致异常:
do {
    System.out.print("Enter choice [1-6]: ");
    while (!s.hasNextInt()) {
        System.out.println("Invalid input, please enter a number");
        s.next();
    }
    user_choice = s.nextInt();
} while (user_choice <1 || user_choice >6);
  • 封装性优化:Bookstore的totalBooks变量可通过books.size()(用ArrayList时)获取,无需单独维护;成员变量添加private修饰并提供必要getter。

  • 异常处理:添加try-catch块处理输入非数字、价格为负数等异常情况,提升程序健壮性。

内容的提问来源于stack exchange,提问作者Subhan khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:10:25