博客
关于我
java编程常见类型题 --- IO文件操作、程序逻辑(百钱百鸡)、 集合应用
阅读量:324 次
发布时间: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/

你可能感兴趣的文章
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>