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

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

java编程常见类型题


IO文件操作

在这里插入图片描述

import java.io.*;public class Exam1 {       public static void main(String[] args) {           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 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();                }            }        }    }}

在这里插入图片描述


程序逻辑:百钱百鸡

在这里插入图片描述

public class Exam2 {       public static void main(String[] args) {           // x 公鸡 只        // y 母鸡 只        // z 小鸡 只        // 5*x + 3*y + (1/3)*z =100 // 总价钱  15x + 9y + z = 300        // x + y + z =100   //总数量             x + y + z = 100        //                                      14x + 8y = 200        // 公鸡        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);                    }                }            }        }     }

在这里插入图片描述


集合应用

在这里插入图片描述

import java.util.HashMap;import java.util.Iterator;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;    }    // 重写toString    @Override    public String toString() {           return "储户id:" + id + '\t' + "姓名" + name + '\t' + "余额" + money;    }}public class Exam3 {       public static void main(String[] args) {           // 创建HashMap        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()){ Object key = iterator.next(); Object value = hashMap.get(key); System.out.println(value); } }}

在这里插入图片描述

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

你可能感兴趣的文章
杭电 2007 平方和与立方和(输入数据的大小顺序并不能默认)
查看>>
十大排序算法之三:插入排序(Python)
查看>>
利用递归实现二叉树的前中后序遍历(Python)
查看>>
合并两个有序数组
查看>>
聊聊我的五一小假期
查看>>
Vue新建项目——页面初始化
查看>>
Node.js包使用系列(一)——修改NPM全局下载和缓存路径
查看>>
CSS position属性static/relative/absolute/fixed/sticky用法总结
查看>>
6.14编一个程序,将两个字符串s1和s2比较,不要用strcmp函数。
查看>>
Java纯文本文件显示工具制作
查看>>
三、案例:留言板 & url.parse()
查看>>
Python实验26:计算文件MD5值
查看>>
LeetCode:28. 实现 strStr()——————简单
查看>>
Nginx配置反向代理与负载均衡
查看>>
Lionheart万汇:布林线双底形态分析技巧
查看>>
Mybatis的入门01
查看>>
Vue使用bus进行组件间、父子路由间通信
查看>>
数据库三个级别封锁协议
查看>>
类的实例
查看>>
tomcat加载部署webapps目录下的项目
查看>>