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

主类调用LibraryCollection的findMaterial方法无输出,如何正确调用?

解决图书馆管理系统中findMaterial()无输出的问题

我来帮你排查一下问题所在,主要有两个核心原因导致控制台没有输出:


问题分析

  1. 传入了null作为搜索参数:你的代码里search变量是null,而findMaterial()方法里如果检测到参数为null会直接返回null,没有任何输出语句,所以控制台不会有内容。
  2. LibraryCollection中没有任何Material对象:你初始化了LibraryCollection的数组,但从来没有往里面添加过Material实例,数组里全是null,就算传入有效的ID也找不到对应内容,同样不会有输出。

解决方案

我们需要两步来修复这个问题:给LibraryCollection添加添加物料的方法,然后在主类中正确初始化数据并调用方法。

1. 给LibraryCollection添加addMaterial()方法

首先,在LibraryCollection类中添加一个方法,用来往集合里添加Material对象:

public boolean addMaterial(Material material) {
    // 遍历数组找空位置添加
    for (int i = 0; i < libraryCollection.length; i++) {
        if (libraryCollection[i] == null) {
            libraryCollection[i] = material;
            return true;
        }
    }
    System.out.println("Collection is full, cannot add more materials.");
    return false;
}

2. 优化findMaterial()方法(可选但推荐)

为了让方法更友好,我们可以在参数为null或者找不到物料时添加控制台提示:

public Material findMaterial(String theFindMaterial) {
    if(theFindMaterial == null) {
        System.out.println("Error: Search material ID cannot be null.");
        return null;
    }
    for(int i = 0; i < libraryCollection.length; i++) {
        if(libraryCollection[i] !=null && theFindMaterial.equals(libraryCollection[i].getMaterialId())) {
            return libraryCollection[i];
        }
    }
    System.out.println("Material with ID '" + theFindMaterial + "' was not found in the collection.");
    return null;
}

3. 修改主类的调用代码

现在在主方法中,先创建并添加Material实例,再调用findMaterial()和checkOutMaterial()方法:

public class LibraryMain {
    public static void main(String[] args) {
        // 初始化容量为10的图书馆集合
        LibraryCollection librarycollectObj1 = new LibraryCollection(10);
        
        // 创建几个Material实例并添加到集合中
        Material book1 = new Material(Material.getNextID(), "Java Programming");
        Material book2 = new Material(Material.getNextID(), "Data Structures & Algorithms");
        librarycollectObj1.addMaterial(book1);
        librarycollectObj1.addMaterial(book2);
        
        // 测试findMaterial方法
        String searchId = "1"; // 对应第一个创建的Material的ID
        Material foundMat = librarycollectObj1.findMaterial(searchId);
        if (foundMat != null) {
            System.out.println("\nFound Material Details:");
            System.out.println(foundMat);
        }
        
        // 测试checkOutMaterial方法
        System.out.println("\n--- Testing Check Out ---");
        boolean checkOutSuccess = librarycollectObj1.checkOutMaterial("1", "1001");
        if (checkOutSuccess) {
            System.out.println("Check out completed successfully!");
            // 打印更新后的物料信息
            System.out.println("\nUpdated Material Info:");
            System.out.println(librarycollectObj1.findMaterial("1"));
        }
    }
}

运行效果

现在运行主类,控制台会输出类似这样的内容:

Found Material Details: 
Material ID: 1 
Material Title: Java Programming 
Checked Out: false 
Patron check out: 0

--- Testing Check Out ---
Check out completed successfully!

Updated Material Info: 
Material ID: 1 
Material Title: Java Programming 
Checked Out: true 
Patron check out: 1001

这样就能正常看到方法调用的结果啦!

内容的提问来源于stack exchange,提问作者Abdul Sultani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:02:27