首页 > 其他分享 >练习题11-

练习题11-

时间:2022-10-23 23:33:44浏览次数:58  
标签:练习题 11 java map int new import public

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;
    }
}

标签:练习题,11,java,map,int,new,import,public
From: https://www.cnblogs.com/wyzel/p/16820038.html

相关文章

  • 练习题10-
    1、模拟上车案例:有一辆车,有100个座位,有前中后三个门每个门都可以随机上人,上人的个数不确定。packagecom.xxx;importjava.util.Random;publicclassCarRunnable......
  • 练习题09-Proerties、IO
    案例需求:在Properties文件中手动写上姓名和年龄,读取到集合中,将该数据封装成学生对象,写到本地文件实现步骤:1.创建Properties集合,将本地文件中的数据加载到集合中2.获......
  • 练习题08File、IO
    1、创建一个文件文件中的内容是name=张三age=12pwd=234读取文件在控制台打印张三12234packagecom.xxx;importjava.io.FileInputStream;importjava.io.IOExc......
  • 练习题07File、Map
    1、请使用Map集合存储自定义数据类型Car做键,对应的价格做值。并使用keySet和entrySet两种方式遍历Map集合。packagecom.xxx;publicclassCar{privateStringna......
  • UE4学习笔记11——【蓝图】拾取钥匙开门
    P35.拾取钥匙开门P35(在“内容浏览器”中,“内容——StartContent——Architecture”中有一些关于建筑的静态网格体,比如墙)(复制一个我们之前做好的旋转开关门蓝图类,在这......
  • C语言的练习题
    有1,2,3,4四个数字,那能组成多少个互不相同且无重复数字的三位数?都是多少?分析:三位数可表示为:个位:g,十位:s,百位:b.可以有多少组合:用for语句的嵌套#include<stdio.h>intmain(......
  • 11、什么是中断?
    在计算机中,中断是系统用来响应硬件设备请求的一种机制,操作系统收到硬件的中断请求,会打断正在执行的进程,然后调用内核中的中断处理程序来响应请求。举个生活中取外卖的例子,可......
  • 项目选java8报错 -- 警告: 源发行版 11 需要目标发行版 11 -- 解决方法
    项目创建时默认是JDK11,选JDK8作为SDK,运行时报错,提示“警告:源发行版11需要目标发行版11”,解决方法:点击菜单File-->Settings-->Build,...-->Compiler-->Jav......
  • 关于debian11无法安装星火商店的解决方法
    #!/bin/bashif["$(id-u)"!="0"]then echo"请确保你使用root权限启动此脚本" exitfiecho"此脚本会加入debiantesting到你的sources.list,这样就可以正常安......
  • 2022-2023-1 20221311 《计算机基础与程序设计》第八周学习总结
    班级链接:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK08作业目标:学习计算机科学概论第9章《C语......