美文网首页
双色球选号程序(非面向对象)

双色球选号程序(非面向对象)

作者: _Raye | 来源:发表于2017-04-04 11:19 被阅读0次
    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
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:双色球选号程序(非面向对象)

          本文链接:https://www.haomeiwen.com/subject/vemaottx.html