Java基于Set集合实现体彩大乐透2.0

news/2024/10/25 20:22:44/

编写程序实现体彩大乐透机选5注

题目

编写程序实现体彩大乐透机选五注,把机选结果打印在控制台上,具体规则如下

  1. 大乐透由前区和后区构成
  2. 前区:从1-35中随机选出不重复的5个数
  3. 后区:从1-12中随机选出不重复的2个数
Set集合实现就不用考虑重复的问题,不像1.0LinkedList集合就需要考虑是否重复,还要去重。而Set可以存储唯一、无序的对象。
创建Set集合对象
//集合类型为Int
Set<Integer> list = new HashSet<Integer>();
给集合添加数据
Set<Integer> list = new HashSet<Integer>();
while (list.size() != count) {
// 范围(max-min+1)+1list.add(rand.nextInt(max - min + 1) + 1);
}
Integer[] array = new Integer[list.size()];
//转成数组
list.toArray(array);
for (int i = 0; i < array.length; i++) {System.out.print(array[i]+"  ");
}
//		System.out.print(list);
完整代码
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Random;
import java.util.Set;public class CaiPiao2 {static Random rand = new Random();public static void main(String[] args) {int sum =0;do {System.out.print("第"+(sum+1)+"注彩票为:");listAdd(1, 35, 5);listAdd(1,12,2);System.out.println();sum++;}while(sum<5);}//定义方法,最大值,最小值,一次取出多少个数 private static void listAdd(int min, int max, int count) {Set<Integer> list = new HashSet<Integer>();while (list.size() != count) {// 范围(max-min+1)+1list.add(rand.nextInt(max - min + 1) + 1);}Integer[] array = new Integer[list.size()];//转成数组list.toArray(array);for (int i = 0; i < array.length; i++) {System.out.print(array[i]+"  ");}
//		System.out.print(list);}
}
运行结果

在这里插入图片描述


http://www.ppmy.cn/news/966518.html

相关文章

html中使用js实现体彩大乐透随机选号

体彩大乐透随机选号 页面预览&#xff1a; 代码实现&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>体彩大乐透随机选号</title><script>function start() {var arr1 ne…

c语言七星彩票兑奖7位数,7位数的体彩!具体中奖怎么算!

满意答案 【江苏体育彩票七位数&#xff0c;中奖规则;】特等奖:彩票7位数号码与中奖号码相同且排列一致&#xff0c;如1234567。 一等奖:彩票号码中连续6位数号码与中奖号码相同位置的连续6位数相同&#xff0c;如123456*或*234567。 二等奖:彩票号码中连续5位数号码与中奖号码…

体彩python_【Python】体育彩票选号器

from tkinter import * importtkinter.ttk as tkimportdatetimefrom tkinter.messagebox import * importtkinter.font as tfimporttimeimportrandomimportthreadingclassSportslottery(Tk):def __init__(self): super().__init__() self.title("体育彩票选号器") se…

Java基于集合实现体彩大乐透1.0

编写程序实现体彩大乐透机选5注 题目 编写程序实现体彩大乐透机选五注&#xff0c;把机选结果打印在控制台上&#xff0c;具体规则如下 大乐透由前区和后区构成前区&#xff1a;从1-35中随机选出不重复的5个数后区&#xff1a;从1-12中随机选出不重复的2个数 创建int型集合…

体彩大乐透中奖提醒程序

近来在体彩大乐透追一注号码&#xff08;这是我一夜暴富的唯一可能了&#xff09;&#xff0c;发现各大彩票网都不提供中奖提醒功能&#xff0c;这么容易实现的功能&#xff0c;它们竟然不提供&#xff0c;不知道为什么。每期自己对奖多麻烦啊&#xff0c;于是用python自己写了…

1.中国体彩(模拟)

中国体彩 前言 彩票太难中&#xff0c;写个代码天天给自己开一注 。自己开的注中了一等奖咱就买个烤肠吃。 可给自己开注以来&#xff0c;二狗已经三月没吃烤肠了。 一、大乐透的选数规则是什么&#xff1f; 在写之前&#xff0c;咱要先简单的搞懂选数规则。 大乐透分为前区五…

体彩数据爬取

大乐透 爬取1 # 爬取大乐透的开奖历史数据 # http://www.lottery.gov.cn/api/lottery_kj_detail_new.jspx?_ltype=4&_term=19026 import requests import re import csv agent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko)…

体彩开奖结果获取

Java爬虫有很多&#xff0c;WebMagic是其中一个&#xff0c;文档齐全&#xff0c;入门简单&#xff0c;个人用来爬取一些小数据很不错&#xff0c;以下以爬取彩票开奖结果为例&#xff0c;介绍一下基本用法。 WebMagic官网文档Introduction WebMagic Documents&#xff0c;文…