双色球选号程序(非面向对象)
2017-04-04 本文已影响0人
_Raye
package org.mobiletrain;
import java.util.Scanner;
public class Test01 {
private static void bubbleSort(int [] array) {
boolean swapped = true;
for(int i = 1; swapped && i < array.length; i++) {
swapped = false;
for (int j = 0; j < array.length - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("机选几注:");
int n = input.nextInt();
input.close();
for(int counter = 1; counter <= n;counter++) {
int[] redBalls = new int[6];
for (int i = 0; i < redBalls.length;) {
//生成1~33的随机数,作为红色球的号码
int number = (int) (Math.random() * 33 + 1);
//检查此号码在之前选中的号码中有没有出现过
boolean isDuplicated = false;
for (int j = 0; j < i; j++) {
//如果当前号码已经出现过
if (redBalls[j] == number) {
isDuplicated = true;
break;
}
}
if (!isDuplicated) {
redBalls[i] = number;
i++;//只有选中不重复的,i才+1.
}
}
bubbleSort(redBalls);
for(int x: redBalls){
System.out.printf("%02d ", x);
}
int blueBall = (int) (Math.random() * 16 + 1);
System.out.printf("(%02d)\n",blueBall);//%02d表示,数字不够两位,前面补0
}
}
}