public class NumberAutoGenerator {
public static void main(String[] args) {
NumberAutoGenerator fun = new NumberAutoGenerator();
fun.generateRed();
fun.generateBlue();
fun.showResult();
}
private int[] redBalls = new int[6];
private int blueBall = 0;
public void run() {
this.generateRed();
this.generateBlue();
this.showResult();
}
void generateRed() {
for (int i = 0; i < this.redBalls.length; i++) {
this.redBalls[i] = this.generateRandomNum(33);
for (int j = 0; j < i; j++) {
if (this.redBalls[j] == this.redBalls[i]) {
i--;
break;
}
}
}
}
void generateBlue() {
this.blueBall = this.generateRandomNum(16);
}
// 设计工具方法的思想
private int generateRandomNum(int initNum) {
return (int) (Math.random() * initNum + 1);
}
void showResult() {
System.out.print("红球:");
for (int i = 0; i < this.redBalls.length; i++) {
System.out.print(this.redBalls[i]);
if (i < this.redBalls.length - 1) {
System.out.print(",");
}
}
System.out.println();// 打印换行符
System.out.println("蓝球:" + this.blueBall);
}
}