博客
关于我
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/

你可能感兴趣的文章
linux网络编程系列(十二)--滑动窗口、拥塞控制、断线重连机制
查看>>
Deep residual learning for image recognition
查看>>
IO控制器
查看>>
LeetCode122.买卖股票的最佳时机2Golang版
查看>>
Java 知识点总结篇(2)
查看>>
Python 知识点总结篇(2)
查看>>
Python 知识点总结篇(3)
查看>>
Numpy 如何操作数组
查看>>
爬取网易科技滚动新闻
查看>>
vuex modules
查看>>
Java笔记:单链表
查看>>
Java基础题:小根堆为8,15,10,21,34,16,12,删除关键字8之后需重建堆,需要的比较次数为?
查看>>
phthon基本语法——温习
查看>>
sleep、wait、yield、join——简介
查看>>
web项目配置
查看>>
VTK:Medical之MedicalDemo2
查看>>
c语言(基本数据类型)实参与形参传值 用汇编理解
查看>>
基于单片机可控音乐流水灯控制设计-全套资料
查看>>
基于单片机简易信号误差分析设计-全套资料
查看>>
基于单片机简易脉搏测量仪系统设计-毕设课设资料
查看>>