• 实训9.2.作业1.写一个10次循环,每次得到一个随机数,放进一个集合中,如果这个数已经存在集合中则跳过,最后打印集合中的数字.


    1.随机产生10个1-100之间的数放到List中,要求这10个数不能有重复,并且都是偶数(添加10次,可能循环很多次)

     
            static void Main(string[] args)
            {//随机产生10个1-100之间的数放到List中,要求这10个数不能有重复,并且都是偶数(添加10次,可能循环很多次)
                List<int> list = new List<int>();
                Random r = new Random();//放到循环外面会好一些,避免生成一些垃圾
                while (list.Count<10)
                {
                    int num = r.Next(1, 101);
                    if (!list.Contains(num)&&num%2==0)//集合里面没有这个数字 并且这个数字是偶数 ,则执行list.Add()语句
                    {
                        list.Add(num);
                    }
                }
                for(int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(list[i]);
                }
                Console.ReadKey();
     
     
            }


    2.集合框架(产生10个1-20之间的随机数要求随机数不能重复)

    //集合框架(产生10个1-20之间的随机数要求随机数不能重复)
        /*创建随机数
         * 用hashset存储
         * 1-20之间的随机数要求随机数不能重复
         * 如果hashset的size小于10 就可以不断的存储,如果大于等于10就停止存储
         * 通过random类中的nextint(n)方法来获取1到20之间的随机数存储到hashsat中
         * 遍历hashset
         *
         * */
        //创建hashset
        HashSet<Integer> b =new HashSet<>();
        
        //创建随机数对象
        Random a =new Random();
        
        //如果hashset的size小于10 就可以不断的存储
        while (b.size()<10) {
            b.add(a.nextInt(20)+1);
            
        }
        //遍历hashset
        for (Integer integer : b) {
            System.out.println(integer);
            
        }
        
        }
    
    ————————————————
    版权声明:本文为CSDN博主「WHJPIECE」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/wanghonhjunking/article/details/73480636

     3.Java]随机生成5个1~10之间的随机数,存入一个List集合中,编写方法对List进行排序

    package com.sj.zy;
    import java.util.ArrayList;
    import java.util.Arrays;
    public class ListRandomSort {
        public static void main(String[] args) {
            ArrayList<Integer> list   = new ArrayList<Integer>();
            int radom=0;
            for (int i = 0; i < 5; i++) {
                radom = (int)(1+Math.random()*(10-1+1));
                list.add(i,radom);            
            }
                ListSort(list);
        }
        public static void ListSort(ArrayList<Integer> list) {//ListSort排序方法
            int [] a = new int[list.size()];
            //将list 中的数据复制一份给当前数组
            for (int i = 0; i < a.length; i++) {
                a[i]= list.get(i);        
            }
            //对数组进行排序:
            Arrays.sort(a);
            //将排好顺序的数组a中的数据复制一份给list
                for (int j = 0; j < a.length; j++) {
                    list.add(j,a[j]);
                }            
                for (int i = 0; i < a.length; i++) {
                    System.out.println(list.get(i));
                }
        }
     
    }

    4.(集合)获取10个1-20之间的随机数,要求不能重复

    import java.util.ArrayList; 
    import java.util.Random;

    /* 
    * 获取10个1-20之间的随机数,要求不能重复 

    * 用数组实现,但是数组的长度是固定的,长度不好确定。 
    * 所以我们使用集合实现。 

    * 分析: 
    * A:创建产生随机数的对象 
    * B:创建一个存储随机数的集合。 
    * C:定义一个统计变量。从0开始。 
    * D:判断统计遍历是否小于10 
    * 是:先产生一个随机数,判断该随机数在集合中是否存在。 
    * 如果不存在:就添加,统计变量++。 
    * 如果存在:就不搭理它。 
    * 否:不搭理它 
    * E:遍历集合 
    */ 
    public class RandomDemo { 
    public static void main(String[] args) { 
    // 创建产生随机数的对象 
    Random r = new Random();

        // 创建一个存储随机数的集合。
        ArrayList<Integer> array = new ArrayList<Integer>();
    
        // 定义一个统计变量。从0开始。
        int count = 0;
    
        // 判断统计遍历是否小于10
        while (count < 10) {
            //先产生一个随机数
            int number = r.nextInt(20) + 1;
    
            //判断该随机数在集合中是否存在。
            if(!array.contains(number)){
                //如果不存在:就添加,统计变量++。
                array.add(number);
                count++;
            }
        }
    
        //遍历集合
        for(Integer i : array){
            System.out.println(i);
        }
    }

    作业1

    package com.java.util;
    import java.util.ArrayList; 
    import java.util.Random;
    public class RandomDemo { 
    public static void main(String[] args) { 
    // 创建产生随机数的对象 
    Random r = new Random();
    // 创建一个存储随机数的集合。
        ArrayList<Integer> array = new ArrayList<>();
    
        // 定义一个统计变量。从0开始。
        int count = 0;
    
        // 判断统计遍历是否小于10
        while (count < 10) {
            //先产生一个随机数
            int number = r.nextInt(20) + 1;
    
            //判断该随机数在集合中是否存在。
            if(!array.contains(number)){
                //如果不存在:就添加,统计变量++。
                array.add(number);
                count++;
            }
        }
    
        //遍历集合
        for(Integer i : array){
            System.out.println(i);
        }
    }
  • 相关阅读:
    线程同步
    子程序循环10次,接着主程序循环100次,然后子程序又循环10次,主程序循环100次,这样循环50次
    MissingNumber缺失的数字,FirstMissingPositive第一个缺失的正数
    数字组合问题:Combination,CombinationSum,CombinationSum2,CombinationSum3
    Count and Say,统计并输出,利用递归,和斐波那契数列原理一样。
    Sudoku Solver, 求数独
    Valid sudoku, 是否是有效的数独
    Search insert position, 查找插入位置
    用excel来做项目管理?
    Redis 在 SNS 类应用中的最佳实践有哪些?
  • 原文地址:https://www.cnblogs.com/xtxt1127/p/11449609.html
Copyright © 2020-2023  润新知