Ball类:
class Ball{
private String color;
private int num;
public Ball(int num, String color){
this.num = num;
this.color = color;
}
public Color getColor(){
return color;
}
public Num getNum(){
return num;
}
}
public static void main(String[] args){
for(int i = 0; i < 6; i++){
int num = (int)(Math.random * 33 + 1);
Ball redBall = new Ball(num,red);
int number = redBall.getNum();
System.out.print(number + " ");
}
int num1 = (int)(Math.random * 16 + 1);
Ball blueBall = new Ball(num1,blue);
int number1 = blueBall.getNum();
System.out.print(number1);
}
选号:
import java.awt.Color;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
class Ball implements Comparable<Ball> {
private int number;
private Color color;
public Ball(int number, Color color) {
this.number = number;
this.color = color;
}
public int getNumber() {
return number;
}
public Color getColor() {
return color;
}
@Override
public int compareTo(Ball other) {
return this.number - other.number;
}
}
class SelectNumberMachine {
private static Random r = new Random();
private List<Ball> redBalls = new ArrayList<>();
private List<Ball> blueBalls = new ArrayList<>();
public void reset() {
redBalls.clear();
for (int i = 1; i <= 33; ++i) {
redBalls.add(new Ball(i, Color.RED));
}
blueBalls.clear();
for (int i = 1; i <= 16; ++i) {
blueBalls.add(new Ball(i, Color.BLUE));
}
}
public String generate() {
Ball[] currentRedBalls = selectRedBalls();
Arrays.sort(currentRedBalls);
Ball currentBlueBall = selectBlueBall();
StringBuilder sb = new StringBuilder();
for (Ball tempBall : currentRedBalls) {
sb.append(String.format("%02d ", tempBall.getNumber()));
}
sb.append("| ");
sb.append(String.format("%02d", currentBlueBall.getNumber()));
return sb.toString();
}
private Ball[] selectRedBalls() {
Ball[] currentRedBalls = new Ball[6];
for (int i = 0; i < currentRedBalls.length; ++i) {
int randomIndex = r.nextInt(redBalls.size());
currentRedBalls[i] = redBalls.remove(randomIndex);
}
return currentRedBalls;
}
private Ball selectBlueBall() {
int randomIndex = r.nextInt(blueBalls.size());
return blueBalls.remove(randomIndex);
}
}
class Test {
public static void main(String[] args) {
SelectNumberMachine machine = new SelectNumberMachine();
for (int i = 0; i < 10; ++i) {
machine.reset();
System.out.println(machine.generate());
}
}
}
网友评论