import java.util.*;
// 双色球
public class DoubleColor {
public static void main(String[] args) {
// LinkedHashMap<Integer,Ball> hashSet = new LinkedHashMap<>();
ArrayList<Ball> al = new ArrayList<>();
al.add(new Ball("红", prodRandRedNum()));
al.add(new Ball("红", prodRandRedNum()));
al.add(new Ball("红", prodRandRedNum()));
al.add(new Ball("红", prodRandRedNum()));
al.add(new Ball("红", prodRandRedNum()));
al.add(new Ball("红", prodRandRedNum()));
System.out.println(al);
Collections.sort(al);
System.out.println(al);
al.add(new Ball("蓝", prodRandBlueNum()));
System.out.println(al);
}
/**
* 输出一个范围是 1 - 33 的值
*
* @return int
*/
public static int prodRandRedNum() {
Random random = new Random();
// System.out.println(r);
return random.nextInt(33) + 1;
}
public static int prodRandBlueNum() {
Random random = new Random();
// System.out.println(r);
return random.nextInt(16) + 1;
}
}
ball类
package todayHw4;
public class Ball implements Comparable<Ball>{
private String color;
private Integer num;
@Override
public int compareTo(Ball o) {
// 升序,排序
// this.getAge() - o.getAge()
/*if(o.getColor().equals("蓝")){
return -1;
}else{
}*/
return this.getNum() - o.getNum();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return "[" + color + num + "]";
}
public Ball(String color, Integer num) {
this.color = color;
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
}
方法二
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class DoubleColor2 {
public static void main(String[] args) {
// 创建一个容器中存储6个数字,6个数字中最多重复3个
// int[] arr = new int[6];
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < 6; i++) {
//arr[i] = prodRandNum();
if (i < 3) {
al.add(prodRandNum());
//al.add(1);
}
if (i == 3) {
// 需要判断重复
int flag = 1; // 标志位
while (flag > 0) {
// 生成一个随机数 1 或 2
int temp = prodRandNum();
// 重复值计数器
int count = 0;
// 和之前的数字进行循环判断
for (int j = 0; j < al.size(); j++) {
// 如果此次产生的随机数 等于 之前的随机数
if (temp == al.get(j)) {
// 计数器 +1
count++;
}
}
// 如果循环结束后计数器 == 3
if (count == 3) {
System.out.println("重复了3次");
} else {
// 如果循环结束后 计数器不等于3,则没有重复值
al.add(temp);
flag = -1;
}
}
} else if (i == 4) {
// 需要判断重复
int flag = 1; // 标志位
while (flag > 0) {
// 生成一个随机数 1 或 2
int temp = prodRandNum();
// 重复值计数器
int count = 0;
// 和之前的数字进行循环判断
for (int j = 0; j < al.size(); j++) {
// 如果此次产生的随机数 等于 之前的随机数
if (temp == al.get(j)) {
// 计数器 +1
count++;
}
}
// 如果循环结束后计数器 == 3
if (count == 3) {
System.out.println("重复了3次");
} else {
// 如果循环结束后 计数器不等于3,则没有重复值
al.add(temp);
flag = -1;
}
}
} else if (i == 5) {
// 需要判断重复
int flag = 1; // 标志位
while (flag > 0) {
// 生成一个随机数 1 或 2
int temp = prodRandNum();
// 重复值计数器
int count = 0;
// 和之前的数字进行循环判断
for (int j = 0; j < al.size(); j++) {
// 如果此次产生的随机数 等于 之前的随机数
if (temp == al.get(j)) {
// 计数器 +1
count++;
}
}
// 如果循环结束后计数器 == 3
if (count == 3) {
System.out.println("重复了3次");
} else {
// 如果循环结束后 计数器不等于3,则没有重复值
al.add(temp);
flag = -1;
}
}
}else if(i == 6){
al.add(noDupNum(al));
}
}
Collections.sort(al);
System.out.print("红球: ");
for (int i : al) {
System.out.print(i + " ");
}
System.out.print(" 蓝球 ");
al.add(prodRandBlueNum());
System.out.println(al.get(6));
}
public static int prodRandNum() {
Random random = new Random();
// System.out.println(r);
return random.nextInt(32) + 1;
}
public static int prodRandBlueNum() {
Random random = new Random();
// System.out.println(r);
return random.nextInt(16) + 1;
}
/**
* 删除重复值... 可以替换上面的重复代码
* @param arrayList
* @return
*/
public static int noDupNum(ArrayList<Integer> arrayList) {
// 需要判断重复
while (true) {
// 生成一个随机数 1 或 2
int temp = prodRandNum();
// 重复值计数器
int count = 0;
// 和之前的数字进行循环判断
for (Integer integer : arrayList) {
// 如果此次产生的随机数 等于 之前的随机数
if (temp == integer) {
// 计数器 +1
count++;
}
}
// 如果循环结束后计数器 == 3
if (count == 3) {
System.out.println("重复了3次");
} else {
// 如果循环结束后 计数器不等于3,则没有重复值
return temp;
}
}
}
}
第三种方法
import java.util.ArrayList;
import java.util.Collections;
public class DoubleBall {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
int i = 0;
while(i < 6){
int count = 0;
int a = (int)((Math.random()*33)+1);
for(int j:list){
if(a== j){
count++;
}
}
if(count!=3){
list.add(a);
i++;
}
}
// 升序排序
Collections.sort(list);
System.out.print("红球 ");
for(int k:list){
System.out.print( k +" ");
}
int b = (int) (Math.random()*16 )+1;
System.out.print(" 蓝球 " + b);
}
}
5846

被折叠的 条评论
为什么被折叠?



