zoukankan      html  css  js  c++  java
  • 双色球的简易实现

    简单的实现双色球的机选

     1 package com.example.ball;
     2 
     3 import java.util.Arrays;
     4 import java.util.Random;
     5 import java.util.Scanner;
     6 
     7 import android.os.Bundle;
     8 import android.app.Activity;
     9 import android.view.Menu;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 import android.widget.TextView;
    14 import android.widget.Toast;
    15 
    16 public class MainActivity extends Activity {
    17     // 定义控件
    18     private Button btn;
    19     // TextView tv;
    20     // 第一个会执行的方法
    21     private TextView tv;
    22     // 球裤
    23     private String[] redBalls = { "01", "02", "03", "04", "05", "06", "07",
    24             "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
    25             "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
    26             "30", "31", "32", "33" };
    27     private String[] blueBalls = { "01", "02", "03", "04", "05", "06", "07",
    28             "08", "09", "10", "11", "12", "13", "14", "15", "16" };
    29     // 定义一个生成随机数的工具类
    30     Random random = new Random();
    31 
    32     // private Scanner scanner;
    33     @Override
    34     protected void onCreate(Bundle savedInstanceState) {
    35         super.onCreate(savedInstanceState);
    36         // 加载当前活动的布局
    37         setContentView(R.layout.activity_main);
    38         // 加载控件
    39         btn = (Button) findViewById(R.id.button1);// 强转为button
    40         tv = (TextView) findViewById(R.id.textView1);
    41 
    42         btn.setOnClickListener(new OnClickListener() {
    43 
    44             @Override
    45             public void onClick(View v) {
    46                 checkBall();// 选第二个
    47 
    48             }
    49         });
    50         // scanner = new Scanner(System.in);
    51     }
    52 
    53     protected void checkBall() {
    54         // 定义一个数组保存生成的红球号码
    55         String checkRed[] = new String[6];
    56         String checkBlue = "";
    57         // 定义一个boolean数组保存生成过得数据
    58         boolean isChecked[] = new boolean[33];
    59         // 位置
    60         int position = -1;
    61         for (int i = 0; i < 6; i++) {
    62             do {
    63                 // 生成的数据是在0-32之间
    64                 position = random.nextInt(33);
    65             } while (isChecked[position]);
    66             isChecked[position] = true;
    67             // position = random.nextInt(33);//包含33不包含0
    68             String checkdRedBall = redBalls[position];
    69             // 保存号码
    70             checkRed[i] = checkdRedBall;
    71         }
    72         checkBlue = blueBalls[random.nextInt(16)];
    73         tv.append("红球:" + Arrays.toString(checkRed) + ";蓝球:" + checkBlue + "
    ");
    74     }
    75 }

    代码生成代码!

  • 相关阅读:
    关于数组的算法-编程之美读后感-1
    java学习笔记之线程1
    java学习笔记之IO一()
    java学习笔记之泛型
    java学习笔记之正则表达式
    Thinking in java学习笔记之String的不可变性
    Thinking in java学习笔记之map的应用
    Thinking in java学习笔记之set
    scrapy之中间件
    Linux之Redis-redis哨兵集群详解
  • 原文地址:https://www.cnblogs.com/Vibge/p/6055445.html
Copyright © 2011-2022 走看看