
java编程常见类型题 --- IO文件操作、程序逻辑(百钱百鸡)、 集合应用
发布日期:2021-05-07 02:47:55
浏览次数:22
分类:精选文章
本文共 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 HashMaphashMap = 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); } }}
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年03月28日 08时00分55秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【IoT】蓝牙BLE基础:CC254x通信系列之模拟SPI协议
2019-03-05
【IoT】TI BLE CC2541 串口控制蓝牙详解
2019-03-05
【产品】项目管理的五个过程和九大知识领域之二
2019-03-05
【项目管理】项目管理流程浅析
2019-03-05
【Tool】如何使用 Uniflash 烧写 WIFI 芯片 CC3200
2019-03-05
copy_{to, from}_user()的思考
2019-03-05
Web前端安全策略之CSRF的攻击与防御
2019-03-05
纯客户端页面关键字搜索高亮jQuery插件
2019-03-05
linux运维中常用的命令
2019-03-05
M1芯片的macbook安装王者荣耀,原神,崩坏方法
2019-03-05
Java温故而知新-反射机制
2019-03-05
eclipse引用sun.misc开头的类
2019-03-05
firefox中angular2嵌套发送请求问题
2019-03-05
【mybatis3】调试/断点打印日志
2019-03-05
C++
2019-03-05
[CTFSHOW]PHP特性
2019-03-05
navigator对象
2019-03-05
关于EFI系统分区(ESP)你应该知道的3件事
2019-03-05
5.Mybatis复杂映射开发
2019-03-05
Servlet2.5的增删改查功能分析与实现------删除功能(四)
2019-03-05