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

如何显示构造方法中创建的数组?销售文件读取表开发求助

如何显示SalesTable构造方法中创建的数组

Great start on your SalesTable class! To display the array you're building in the constructor, here's a straightforward approach that keeps your code clean and organized:

1. 先完成数组的初始化与数据填充

首先,你需要在构造方法里把文件中的实际数据读取并填充到数组中(你的代码里的//...部分就是干这个的)。假设你的销售数据是数值型(比如double类型),先在类里定义一个成员变量来存储数组:

public class SalesTable {
    private int myRows;
    private int myCols;
    private double[][] salesData; // 新增:存储销售数据的二维数组

    public SalesTable(String fileName) {
        Scanner theFile = null;
        try {
            theFile = new Scanner(new File(fileName));
            // 获取行列数
            myRows = theFile.nextInt();
            myCols = theFile.nextInt();
            // 初始化二维数组
            salesData = new double[myRows][myCols];
            // 循环读取文件中的每一个数据,填充到数组
            for (int row = 0; row < myRows; row++) {
                for (int col = 0; col < myCols; col++) {
                    salesData[row][col] = theFile.nextDouble();
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File " + fileName + " not found.\n");
            System.exit(1);
        } finally {
            // 别忘了关闭文件资源
            if (theFile != null) {
                theFile.close();
            }
        }
    }
    // 后续添加显示方法...
}

2. 添加专门的显示方法

给SalesTable类添加一个displaySalesData()方法,负责格式化输出数组内容。这样做符合单一职责原则——构造方法只负责初始化数据,显示逻辑单独抽离:

public void displaySalesData() {
    System.out.println("=== 月度销售数据 ===");
    // 遍历每一行数据
    for (int row = 0; row < myRows; row++) {
        // 遍历当前行的每一列,格式化输出(这里用%10.2f保证列对齐,保留两位小数)
        for (int col = 0; col < myCols; col++) {
            System.out.printf("%10.2f", salesData[row][col]);
        }
        // 每行结束后换行
        System.out.println();
    }
}

如果你的列有具体含义(比如代表1-8月),还可以先打印表头,让输出更清晰:

public void displaySalesData() {
    // 打印表头
    System.out.println("=== 月度销售数据 ===");
    System.out.printf("%12s", "行号");
    for (int month = 1; month <= myCols; month++) {
        System.out.printf("%10s月", month);
    }
    System.out.println();
    // 打印数据行
    for (int row = 0; row < myRows; row++) {
        System.out.printf("%12d", row + 1);
        for (int col = 0; col < myCols; col++) {
            System.out.printf("%10.2f", salesData[row][col]);
        }
        System.out.println();
    }
}

3. 在主程序中调用显示方法

最后,在你的主类里创建SalesTable实例,然后调用显示方法即可:

public class Main {
    public static void main(String[] args) {
        SalesTable salesTable = new SalesTable("sales_data.txt");
        salesTable.displaySalesData();
    }
}

额外优化:用try-with-resources简化资源管理

Java 7+的try-with-resources语法可以自动关闭文件资源,避免手动关闭可能出现的遗漏,让构造方法更简洁:

public SalesTable(String fileName) {
    try (Scanner theFile = new Scanner(new File(fileName))) {
        myRows = theFile.nextInt();
        myCols = theFile.nextInt();
        salesData = new double[myRows][myCols];
        for (int row = 0; row < myRows; row++) {
            for (int col = 0; col < myCols; col++) {
                salesData[row][col] = theFile.nextDouble();
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("File " + fileName + " not found.\n");
        System.exit(1);
    }
    // 无需手动关闭Scanner,try-with-resources会自动处理
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:15:56