目录
ArrayList.java
Ball.java
BallColor.java 这是枚举类型
Test1.java 我的主方法文件
运行截图:编辑
双色球游戏:游戏规则红色球号码从1--33中选择;蓝色球号码从1--16中选择,每注投注号码由6个红色球号码和1个蓝色球号码组成
ArrayList.java
package com.yan4;import java.util.Arrays;public class ArrayList {private Ball[] arr;private int size = 0;public ArrayList() {this(10);}public ArrayList(int length) {arr = new Ball[length];}public void add(Ball data) {arr[size++] = data;if (size >= arr.length)resize();}/*** @param index 需要删除的数据下标*/public Ball delete(int index) {if (index >= size || size < 0)throw new ArrayIndexOutOfBoundsException();Ball res = arr[index];System.arraycopy(arr, index + 1, arr, index, arr.length - index - 1);arr[size - 1] = null;size--;return res;}public void update(int index, Ball data) {if (index >= size || size < 0)throw new ArrayIndexOutOfBoundsException();arr[index] = data;}public Ball[] getData() {Ball[] res = new Ball[size];System.arraycopy(arr, 0, res, 0, size);return res;}public String toString() {return Arrays.toString(arr);}public void resize() {Ball[] res = new Ball[arr.length * 3 / 2];System.arraycopy(arr, 0, res, 0, arr.length);this.arr = res;}public int size() {return this.size;}public Ball[] sort() {Ball[] res = getData();Arrays.sort(res, (o1, o2) -> {int res1 = o1.getColor().compareTo(o2.getColor());if (res1 == 0) {res1 = o1.getNumber() - o2.getNumber();}res1 *= -1;return res1;});return res;}}
Ball.java
package com.yan4;public final class Ball {private final int number;private final BallColor color;public Ball(int num, BallColor color) {this.number = num;this.color = color;}public int getNumber() {return number;}public BallColor getColor() {return color;}@Overridepublic String toString() {return "(" + number + "," + color + ")";}}
BallColor.java 这是枚举类型
package com.yan4;public enum BallColor {RED("红球"), BLUE("蓝球");private String name;private BallColor(String name) {this.name = name;}@Overridepublic String toString() {return this.name;}
}
Test1.java 我的主方法文件
package com.yan4;import java.util.Arrays;
import java.util.Random;public class Test1 {public static void main(String[] args) {ArrayList list1 = new ArrayList();for (int i = 1; i <= 33; i++)list1.add(new Ball(i, BallColor.RED));ArrayList list3 = new ArrayList(7);Random r = new Random();for (int i = 0; i < 6; i++) {int pos = r.nextInt(list1.size());Ball data = list1.delete(pos);list3.add(data);}list3.add(new Ball(r.nextInt(16) + 1, BallColor.BLUE));list3.sort();Ball[] res = list3.getData();System.out.println(Arrays.toString(res));}
}
运行截图: