首页 > 其他分享 >大乐透随机

大乐透随机

时间:2022-12-06 10:57:08浏览次数:31  
标签:大乐透 java list5 System list2 随机 groups import

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @Description
 * @Author saq
 * @Date 2022/12/5 15:05
 */
public class Lotto {
    public static void main(String[] args) {
        System.out.println("请输入需要几组:");
        // 输入需要几组
        scanner();
    }

    public static void scanner() {
        // 输入几组
        Scanner scanner = new Scanner(System.in);
        int groups = scanner.nextInt();
        if (groups == 1 || groups == 2 || groups == 3 || groups == 4 || groups == 5) {
            for (int i = 1; i <= groups; i++) {
                System.out.print("第" + i + "组:");
                lotto();
                System.out.println();
            }
        } else {
            System.out.println("输入的数据格式有误,重新输入!");
            scanner();
        }
    }

    public static void lotto() {
        DecimalFormat decimalFormat = new DecimalFormat("00");
        // 保存前五位
        List<Integer> list5 = new ArrayList<>();

        // 前五位
        while (true) {
            Integer front5 = (int) (Math.random() * 35);
            if (front5 > 0 && !list5.contains(front5)) {
                list5.add(front5);
            }
            if (list5.size() == 5) {
                break;
            }
        }
        // 前五位排序
        Collections.sort(list5);

        // 保存后两位
        List<Integer> list2 = new ArrayList<>();

        // 后两位
        while (true) {
            Integer after2 = (int) (Math.random() * 12);
            if (after2 > 0 && !list2.contains(after2)) {
                list2.add(after2);
            }
            if (list2.size() == 2) {
                break;
            }
        }
        // 后两位排序
        Collections.sort(list2);

        list5.forEach(m1 -> System.out.print(decimalFormat.format(m1) + " "));
        System.out.print(" + ");
        list2.forEach(m1 -> System.out.print(decimalFormat.format(m1) + " "));
    }
}

 

标签:大乐透,java,list5,System,list2,随机,groups,import
From: https://www.cnblogs.com/sunAnqing/p/16954577.html

相关文章