打印1-100之內的20個隨機數大集合中的偶數

打印1-100之內的20個隨機數大集合中的偶數

package cn.itcast.day04.demo03;

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

/**
 * @author ChenDan
 * @date 2019/7/9 13:19
 */
public class Demo01SelectArray {
    public static void main(String[] args) {
        ArrayList<Integer> big = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
            int num = r.nextInt(100) + 1;
            big.add(num);
        }

        ArrayList<Integer> small = getSmall(big);
        System.out.println(small.size());
        for (int i = 0; i < small.size(); i++) {
            System.out.println(small.get(i));
        }

    }

    public static ArrayList<Integer> getSmall(ArrayList<Integer> big) {
        ArrayList<Integer> small = new ArrayList<>();

        for (int i = 0; i < big.size(); i++) {
            int num = big.get(i);
            if (num % 2 == 0) {
                small.add(num);
            }
        }
        return small;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章