Android 双色球机选算法

来源:互联网 发布:最强淘宝系统笔趣阁 编辑:程序博客网 时间:2024/06/04 18:06
   /*
     * 双色球机选算法
     *
     */
   
    public void doClick(View view) {
  TextView tv1 = (TextView) findViewById(R.id.textView1);
  TextView tv2 = (TextView) findViewById(R.id.textView2);
  
  String[] red = {
    "01","02","03","04","05","06","07","08","09","10",
    "11","12","13","14","15","16","17","18","19","20",
    "21","22","23","24","25","26","27","28","29","30",
    "31","32","33"
  };
  String[] blue = {
    "01","02","03","04","05","06","07","08","09","10",
    "11","12","13","14","15","16"
  };
  
  boolean[] flags = new boolean[red.length];
  String[] redResult = new String[6];
  
  // 选红球
  for(int i=0;i<redResult.length;i++) {
   int index;
   do {
    index = new Random().nextInt(red.length);
   } while(flags[index]);
   
   flags[index] = true;
   
   redResult[i] = red[index];
  }
  
  // 选蓝球
  String blueResult = blue[new Random().nextInt(blue.length)];
  
  // 显示
  tv1.setText(Arrays.toString(redResult));
  tv2.setText(blueResult);
 }
0 0
原创粉丝点击