zoukankan      html  css  js  c++  java
  • Java——Math,Set,List,map相关练习


    声明一个Set集合,只能保存Double类型的数据, 保存10个随机100以内的数, 找出最大值和最小值,打印输出.

    
        public static void main(String[] args) {
    
            Set<Double> set =new HashSet<Double>();
            System.out.println("10个100以内的随机数");
            for(int i=0;i<10;i++){
                set.add(Math.random()*100);
            }
            double max = -Double.MAX_VALUE;
            double min = Double.MIN_VALUE;
            for(Double dou1:set){
                if(dou1>max){
                    max=dou1;
                }
            }
            for(Double dou2:set){
                if(dou2<min){
                    min=dou2;
                }
            }
            System.out.println(set);
            System.out.println("最大值:"+Collections.max(set));
            System.out.println("最小值:"+Collections.min(set));
        }

    创建一个List集合对象,添加20个30以内的随机整数, 不允许重复. 并打印输出

    public static void main(String[] args) {
    
            System.out.println("20个30以内的随机整数:");
            List list = new ArrayList<>();
            while(list.size()!=20){
                int rand = (int)(Math.random()*30);
                if(!list.contains(rand)){
                    list.add(rand);
                }
            }
            Iterator iter = list.iterator();
            while(iter.hasNext()){
                System.out.print(iter.next()+" ");
            }
        }

    1)从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。2)读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入年份(例如1990):");
            int year = sc.nextInt();
            switch(year){
            case 1930:
            case 1950:
                System.out.println("乌拉圭");
                break;
            case 1934:
            case 1938:
            case 1982:
            case 2006:
                System.out.println("该年世界冠军的是:意大利!");
                break;
            case 1954:
            case 1974:
            case 1990:
                System.out.println("德国");
                break;
            case 1958:
            case 1962:
            case 1970:
            case 1994:
            case 2002:
                System.out.println("巴西");
                break;
            case 1966:
                System.out.println("英格兰");
                break;
            case 1978:
            case 1986:
                System.out.println("阿根廷");
                break;
            case 1998:
                System.out.println("法国");
                break;
            default:
                System.out.println("该年没有世界杯");
                break;
            }
    
            System.out.println("输入球队名字(例如巴西):");
            String name = sc.next();
            switch(name){
            case "乌拉圭":
                System.out.println("1930 1950");
                break;
            case "意大利":
                System.out.println("1934 1938 1982 2006");
                break;
            case "德国":
                System.out.println("1954 1974 1990");
                break;
            case "巴西":
                System.out.println("1958 1962 1970 1994 2002");
                break;
            case "英格兰":
                System.out.println("1966");
                break;
            case "阿根廷":
                System.out.println("1978 1986");
                break;
            case "法国":
                System.out.println("1998");
                break;
            default:
                System.out.println("没有获得过冠军!");
                break;
            }
        }

    1)使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述 课程安排。

    2) 增加了一位新老师Allen 教JDBC 3) Lucy 改为教CoreJava put方法
    3) 遍历Map,输出所有的老师及老师教授的课程(Set

    public static void main(String[] args) {
            Map<String, String>map = new HashMap<>();
            map.put("Tom", "CoreJava");
            map.put("John", "Oracle");
            map.put("Susan", "Oracle");
            map.put("Jerry", "JDBC");
            map.put("Jim", "Unix");
            map.put("Lucy", "JSP");
            map.put("Kevin", "JSP");
            map.put("Allen", "JDBC");
    
            map.remove("Lucy");
            map.put("Lucy", "CoreJava");
    
            Set<Map.Entry<String, String>> entryset =map.entrySet();
            Iterator<Map.Entry<String, String>> iter = entryset.iterator();
            while(iter.hasNext()){
                Map.Entry<String, String> entry =iter.next();
                System.out.print("老师名字:"+entry.getKey()+"	");
                System.out.println("课程名字:"+entry.getValue());
            }
    
    
            Set s2 = map.keySet();// 将map的key赋给s2  
            // 利用迭代器遍历  
            Iterator it4 = s2.iterator();  
            while (it4.hasNext()) {  
                Object key = it4.next();  
                // map.get(value):表示map中的值  
                if (map.get(key).equals("JSP"))  
                    System.out.print(key + " 教 " + map.get(key));  
            } 
    
        }

    写一个Student类, 包含属性id[1-30), grade[1-6], score[0-100], 所有属性都是随机生成 ,创建一个Set集合, 保存20个对象, 如果两个对象的id是一样的,则不允许添加. 使用迭代器遍历集合,打印输出对象的信息, 并找出分数最高的同学和分数最低的同学, 最后打印输出最高分和最低分同学信息.

    public static void main(String[] args) {
            Set<Student> set = new HashSet<Student>();
            for (; set.size() != 20;) {
                int id = (int)(Math.random() * 29 + 1);
                int grade = (int)(Math.random() * 6 + 1);
                double score = (int)(Math.random() * 1001) / 10.0;
                set.add(new Student(id, grade, score));
            }
            for (Student student : set) {
                System.out.println("学生:"+student.getId()+",分数:"+student.getScore()+",Grade:"+student.getGrade());
                //System.out.println(student);
            }
            System.out.println("---------------------------");
            Student maxScoreStudent = null;
            Student minScoreStudent = null;
            Iterator<Student> iterator = set.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                if (maxScoreStudent == null) {
                    maxScoreStudent = student;
                    minScoreStudent = student;
                }
                if (student.getScore() > maxScoreStudent.getScore()) {
                    maxScoreStudent = student;
                } 
                if (student.getScore() < minScoreStudent.getScore()) {
                    minScoreStudent = student;
                }
            }
            System.out.println(maxScoreStudent);
            System.out.println(minScoreStudent);
        }
  • 相关阅读:
    汽车知识图谱
    医疗知识图谱与自动问答
    MongoDB(单节点)环境配置
    【字符串-01】
    【双指针-01】
    【排序-01】十大排序算法(Python实现)
    【队列-01】队列题目解析
    【栈-01】栈题目解析
    【贪心-02】贪心算法题目解析
    【贪心-01】贪心算法原理介绍
  • 原文地址:https://www.cnblogs.com/aixing/p/13327724.html
Copyright © 2011-2022 走看看