练习题11-
1、项目需求:请用户从控制台输入信息,程序将信息存储到文件Info.txt中。可以输入多条信息,每条信息存储- -行。当用户输入:”886”时,程序结束。
package com.xxx.title1; import java.io.*; import java.util.Scanner; public class Test { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); FileWriter fw = new FileWriter("info.txt"); String input; do { System.out.println("请输入信息:"); input = scanner.nextLine(); fw.write(input + "\n"); }while (!input.equals("886")); fw.close(); } }
2、我有一个文本文件
score.xt,我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有"Ii'这样的键存在,如果有就改变其实为"100"
score.txt文件内容如下:
zhangsan= 90
lisi= 80
wangwu= 85
package com.xxx.title2; import java.io.*; import java.util.Properties; import java.util.Set; public class Test { public static void main(String[] args) throws IOException { Properties prop = new Properties(); prop.load(new FileInputStream("score.txt")); //stringPropertyNames返回此属性列表中的键集 Set<String> names = prop.stringPropertyNames(); for (String name : names) { if (name.equals("lisi")) { prop.setProperty(name, "100"); } } FileWriter fw = new FileWriter("score.txt"); prop.store(fw,null); fw.close(); } }
3、现在有一个map集合如下:
Map<Integer, String> map = new HashMap<Integer, String>0;
map.put(1, "张三丰");
map:put(2, "周芷若");
map.put(3, "汪峰");
map put(4,灭绝师太");
要求:
1.遍历集合,并将序号与对应人名打印。
2.向该map集合中插入- -个编码为5姓名为李晓红的信息
3.移除该map中的编号为1的信息
4.将map集合中编号为2的姓名信息修改为"周林"
package com.xxx.title3; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Test { public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<>(); map.put(1,"张三丰"); map.put(2,"周芷若"); map.put(3,"汪峰"); map.put(4,"灭绝师太"); // map.put(5,"李晓红"); // map.remove(1); // map.put(2,"周林"); Set<Map.Entry<Integer, String>> entries = map.entrySet(); for (Map.Entry<Integer, String> entry : entries) { System.out.println(entry.getKey() + ": " + entry.getValue()); } map.put(5,"李晓红"); map.remove(1); map.put(2,"周林"); } }
4、定义sym方法,判断数组中的元素值是否对称
代码实现,效果如下所示:
[1, 2, 3, 4, 3, 2, 1]是否对称:true
[1,2,3,4,5,2,1]是否对称:false
package com.xxx.title4; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] ints = {1,2,3,4,3,2,1}; int[] ints1 = {1,2,3,4,5,2,1}; boolean flag = sym(ints); boolean flag1 = sym(ints1); System.out.println(Arrays.toString(ints) + " 是否对称:" + flag); System.out.println(Arrays.toString(ints1) + " 是否对称:" + flag1); } public static boolean sym(int[] ints) { for (int i = 0; i < ints.length/2; i++) { if (ints[i] != ints[ints.length-1-i]){ return false; } } return true; } }
5、分析以下需求,并用代码实现:
(1)打印由7, 8, 9三个数组成的三位数,要求该三位数中任意两位数字不能相同;
(2)打印格式最后的三位数字以空格分隔,如789 798 879 897 978 987.
注:要求使用StringBuilder 来完成
package com.xxx.title5; import java.util.Arrays; public class Test { public static void main(String[] args) { StringBuilder sb = new StringBuilder("78987"); int[] ints = new int[6]; int count = 0; for (int i = 0; i < 3; i++) { StringBuilder sb1 = new StringBuilder(sb.substring(i, (i + 3)).toString()); ints[count++] = Integer.parseInt(sb1.toString()); ints[count++] = Integer.parseInt(sb1.reverse().toString()); } Arrays.sort(ints); for (int anInt : ints) { System.out.print(anInt + " "); } } }
6、计算一下2022-08-16 是星期几
package com.xxx.title6; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Test { public static void main(String[] args) throws ParseException { Scanner scanner = new Scanner(System.in); System.out.println("请输入日期:"); String input = scanner.next(); Date date = new SimpleDateFormat("yyyy-MM-dd").parse(input); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1); } }
7、模拟统计班级考试分数分布情况,分别统计100-80, 79-60, 59-40, 39-0各个阶段的人数。
。定义getScoreList方法, 随机生成50个数字,数字范围从0到100。
。定义countScore方法,统计各个阶段的分数个数。
。定义printCount方法, 打印各个阶段的统计结果。
代码实现,效果如图所示:
100分--80分:8人
79 分-- 60分:6人
59分--40分:10人
39分--0分:26人
package com.xxx.title7; import java.util.ArrayList; import java.util.Random; public class Test { public static void main(String[] args) { ArrayList<Integer> list = getScoreList(); ArrayList<Integer> countList = countScore(list); printCount(countList); } private static void printCount(ArrayList<Integer> countList) { int start = 100; int end = 80; for (int i = 0; i < countList.size(); i++) { System.out.println(start + " 分--" +end + "分 :"+countList.get(i) + "人"); /* 0 -- 100~80 1 -- 80~60 2 -- 59~40 3 -- 39~0 */ if (i == 1) { start -= 21; end -= 20; } else if (i == 2) { start -= 20; end -= 40; } else { start -= 20; end -= 20; } } } private static ArrayList<Integer> countScore(ArrayList<Integer> list) { ArrayList<Integer> countList = new ArrayList<>(); int count100 = 0; int count79 = 0; int count59 = 0; int count39 = 0; for (int i = 0; i < list.size(); i++) { int score = list.get(i); if (score <= 100 && score >=80) { count100++; } else if (score <= 79 && score >= 60) { count79++; } else if (score <= 59 && score >= 40) { count59++; } else { count39++; } } /* 每个分段的人数加入集合 */ countList.add(count100); countList.add(count79); countList.add(count59); countList.add(count39); return countList; } private static ArrayList<Integer> getScoreList() { ArrayList<Integer> list = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 50; i++) { int x = random.nextInt(101); list.add(x); } return list; } }
8、1.使用 lambda表达式分别将以下功能封装到Function对象中
a)求 Integer类型ArrayList中所有元素的平均数
b)将 Map<String,Integer>中value存到ArrayList
已知学生成绩如下
姓名 成绩
岑小村 59
谷天洛 82
渣渣辉 98
蓝小月 65
皮几万 70
3. 以学生姓名为key成绩为value创建集合并存储数据,使用刚刚创建的Function 对象求学生的平均成绩
package com.xxx.title8; import java.util.*; import java.util.function.Function; public class Test { public static void main(String[] args) { Function<ArrayList<Integer>,Integer> f1 = (list) -> { Integer sum = 0; for (Integer i : list) { sum+=i; } return sum/list.size(); }; Function<Map<String,Integer>,ArrayList<Integer>> f2 = (map) -> { Collection<Integer> values = map.values(); ArrayList<Integer> list = new ArrayList<>(); list.addAll(values); return list; }; HashMap<String, Integer> hashMap = new HashMap<>(); hashMap.put("岑小村",59); hashMap.put("古天洛",82); hashMap.put("渣渣辉",98); hashMap.put("蓝小月",65); hashMap.put("皮几万",70); Integer apply = f2.andThen(f1).apply(hashMap); System.out.println("学生平均成绩是:" + apply); } }
9、统计一个文件夹下不同文件类型的的个数
package com.xxx.title9; import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Test { public static void main(String[] args) { File file = new File("d:\\a\\b"); HashMap<String, Integer> map = new HashMap<>(); HashMap<String, Integer> sum = sum(file, map); Set<Map.Entry<String, Integer>> entries = sum.entrySet(); for (Map.Entry<String, Integer> entry : entries) { System.out.println(entry.getKey() + "类型有:" + entry.getValue()); } } public static HashMap<String, Integer> sum(File file, HashMap<String, Integer> map) { File[] files = file.listFiles(); Set<String> set = map.keySet(); for (File f : files) { if (f.isDirectory()) { sum(f, map); } else { System.out.println(f); String key = f.getName().substring(f.getName().indexOf(".") + 1); if (set.contains(key)) { map.put(key, map.get(key) + 1); } else { map.put(key,1); } } } return map; } }
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16820038.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步