博客
关于我
java编程常见类型题 --- IO文件操作、程序逻辑(百钱百鸡)、 集合应用
阅读量:325 次
发布时间:2019-03-04

本文共 3437 字,大约阅读时间需要 11 分钟。

Java编程常见类型题解析

IO文件操作

文件操作是Java编程中十分基础但也十分重要的内容。以下是关于文件操作的实践案例:

文件创建与判断

首先,我们需要创建一个文件:

File file = new File("G://HelloWorld.txt");if (!file.exists()) {    try {        file.createNewFile();        System.out.println("创建文件成功");    } catch (IOException e) {        e.printStackTrace();    }}

接着,我们可以判断文件的类型:

if (file.isDirectory()) {    System.out.println("这是个目录");} else {    System.out.println("这是一个文件");}

文件夹创建

创建文件夹的逻辑与文件创建类似:

File dir = new File("G://IOTest");if (!dir.exists()) {    dir.mkdir();}

文件复制与目录遍历

为了实现文件的复制操作,我们可以使用FileInputStream和FileOutputStream:

FileInputStream in = null;FileOutputStream out = null;try {    in = new FileInputStream("G://HelloWorld.txt");    out = new FileOutputStream("G://IOTest//HelloWorld.txt");    int len = 0;    while ((len = in.read()) != -1) {        out.write(len);    }    System.out.println("文件移动成功");        // 遍历输出文件    System.out.println("IOTest目录下的文件如下:");    File[] list = dir.listFiles();    for (File file1 : list) {        System.out.println(file1.getName());    }} catch (FileNotFoundException e) {    e.printStackTrace();} catch (IOException e) {    e.printStackTrace();} finally {    if (in != null) {        try {            in.close();        } catch (IOException e) {            e.printStackTrace();        }    }    if (out != null) {        try {            out.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

程序逻辑:百钱百鸡

问题描述

公鸡每只15元,母鸡每只9元,小鸡每只1元。已知:

  • 5只公鸡 + 3只母鸡 + (1/3)只小鸡 = 100元
  • 15只公鸡 + 9只母鸡 + 1只小鸡 = 300元
  • 公鸡 + 母鸡 + 小鸡 = 100只

解决思路

通过建立方程组解决问题:

  • 设公鸡数量为x,母鸡数量为y,小鸡数量为z
  • 方程组:
    • 15x + 9y + z = 300
    • 5x + 3y + (1/3)z = 100
    • x + y + z = 100
  • 通过消元法化简方程,最终得到:

    • 14x + 8y = 200
    • x + y + z = 100

    代码实现

    public class Exam2 {    public static void main(String[] args) {        for (int i = 0; i <= 20; i++) {            for (int j = 0; j <= 34; j++) {                for (int k = 0; k <= 100; k++) {                    if (15 * i + 9 * j + k == 300 && i + j + k == 100) {                        System.out.println("公鸡" + i + ", 母鸡" + j + ", 小鸡" + k);                    }                }            }        }    }}

    优化建议

    为了提高效率,可以通过缩小变量范围:

    • 公鸡最多20只(15*20=300)
    • 母鸡最多34只(9*34=306)
    • 小鸡最多100只

    集合应用

    HashMap示例

    以下是一个使用HashMap的实际案例:

    BankAccount类

    class BankAccount {    String id;    String name;    double money;    public BankAccount(String id, String name, double money) {        this.id = id;        this.name = name;        this.money = money;    }    @Override    public String toString() {        return "储户id:" + id + "\t" + "姓名" + name + "\t" + "余额" + money;    }}

    主程序

    public class Exam3 {    public static void main(String[] args) {        HashMap
    hashMap = new HashMap<>(); hashMap.put("101", new BankAccount("101", "祝枝山", 10000.0)); hashMap.put("102", new BankAccount("102", "文征明", 20000.0)); hashMap.put("103", new BankAccount("103", "唐伯虎", 30000.0)); if (hashMap.containsKey("102")) { System.out.println("检索id为102的储户信息如下:\n" + hashMap.get("102")); } System.out.println("遍历哈希表结果如下:"); Iterator
    iterator = hashMap.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); BankAccount value = hashMap.get(key); System.out.println(value); } }}

    代码解释

  • HashMap创建与添加:使用HashMap存储银行账户信息,通过put方法添加记录。
  • 查找元素:通过containsKey方法检查是否存在特定账户。
  • 遍历打印:使用Iterator遍历哈希表,打印所有账户信息。
  • 总结

    以上内容涵盖了Java编程中的文件操作、算法逻辑和集合应用,适合作为编程练习内容。

    转载地址:http://kweq.baihongyu.com/

    你可能感兴趣的文章
    Power BI:如何在 Power Query 编辑器中将 Python 与多个表一起使用?
    查看>>
    power english (3) main text -emotion mastery - focus
    查看>>
    POWER ENGLISH (6) - MODEL
    查看>>
    power english (1) —— passion
    查看>>
    Power English (1) 原文
    查看>>
    power English (3)原文
    查看>>
    POWER ENGLISH(7)- repetition
    查看>>
    SpringBoot中集成SpringBatch详细解析与实战示例(CSV文件读取十万条数据进行业务处理后写入Mysql数据库)
    查看>>
    powerbi 一张表在另外一张表中出现的数量_PowerBi之初步学习笔记
    查看>>
    QGIS怎样设置简体中文以及新建可编辑的多边形的图层
    查看>>
    PowerBuilder 使用自定义事件触发键盘Enter事件
    查看>>
    PowerCreatorCMS UploadResourcePic 任意文件上传漏洞复现
    查看>>
    PowerDesigner 使用的一些技巧(转)
    查看>>
    QGIS在Windows上下载安装与建立空间数据库连接
    查看>>
    PowerDesigner165安装婆姐汉花教程
    查看>>
    PowerDesigner使用教程:设置注释、默认值属性
    查看>>
    PowerDesigner使用教程:不显示背景网格
    查看>>
    PowerDesigner使用教程:创建数据模型以及导出
    查看>>
    PowerDesigner使用教程:右侧工具栏显示/隐藏
    查看>>
    PowerDesigner使用教程:导出sql文件以及解决中文乱码问题
    查看>>