这次闲来无事,又做了一个大乐透机选五注的小程序,继续练手

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace superLotto1
{
    public partial class Form1 : Form
    {
        TextBox [] textBoxArray = new TextBox[35];
        Label[] labelArray = new Label[5];
        int[] a = new int[100];
        public Form1()
        {
            InitializeComponent();

            int j = 0, k = 0;
            for (int i = 0; i < 35; i++) {
                textBoxArray[i] = new System.Windows.Forms.TextBox();
                textBoxArray[i].SuspendLayout();
                textBoxArray[i].Size = new System.Drawing.Size(30, 20);
                textBoxArray[i].TabIndex = i;
                textBoxArray[i].Name = "textBox" + i.ToString();
                textBoxArray[i].ForeColor = Color.Red;

                textBoxArray[i].Location = new System.Drawing.Point(20 + j++ * 50, 20 + k * 30);
                if (j % 7 == 0) {
                    textBoxArray[i].ForeColor = Color.Blue;
                    textBoxArray[i - 1].ForeColor = Color.Blue;
                    labelArray[k] = new System.Windows.Forms.Label();
                    labelArray[k].SuspendLayout(); 
                    labelArray[k].AutoSize = true;
                    labelArray[k].Location = new System.Drawing.Point(256, 24 + k * 30);
                    labelArray[k].Name = "label" + k.ToString();
                    labelArray[k].Size = new System.Drawing.Size(11, 12);
                    labelArray[k].TabIndex = 0;
                    labelArray[k].Text = "+";
                    this.Controls.Add(labelArray[k]);
                    
                    j = 0;
                    k++;
                }

                this.Controls.Add(textBoxArray[i]);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            superLotto();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("恭喜发财,大吉大利!");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string f = @".\out.txt";
            StreamWriter sw = new StreamWriter(f, false);
            sw.WriteLine("     大乐透随机选五注");
            int i = 0;
            for (int j = 0; j < 5; j++) {
                sw.WriteLine("{0}  {1}  {2}  {3}  {4} + {5}  {6}", textBoxArray[i++].Text, textBoxArray[i++].Text,
                    textBoxArray[i++].Text, textBoxArray[i++].Text, textBoxArray[i++].Text, textBoxArray[i++].Text,
                    textBoxArray[i++].Text);            
            }
            sw.Close();
            sw.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string f = @".\out.txt";
            Process.Start(@"c:\windows\notepad.exe", f);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            superLotto();
        }

        private void superLotto() { 
            int i = 0;
            for (int k = 0; k < 5; k++) {
                do
                {
                    createRandom(a, 5, 35);
                } while (checkSame(a, 5));
                sort(a, 5);
                for (int j = 0; j < 5; j++)
                {
                    textBoxArray[i++].Text = a[j].ToString();
                }

                do
                {
                    createRandom(a, 2, 12);
                } while (checkSame(a, 2));
                sort(a, 2);
                for (int j = 0; j < 2; j++)
                {
                    textBoxArray[i++].Text = a[j].ToString();
                }
            }

            for (int j = 0; j < 35; j++)
            {
                if (Convert.ToInt32(textBoxArray[j].Text) < 10)
                {
                    textBoxArray[j].Text = "0" + textBoxArray[j].Text;
                }
            }
        }

        private void createRandom(int [] a, int c, int limit) {
            Thread.Sleep(32);
            Random r = new Random();
            for (int i = 0; i < c; i++) {
                a[i] = r.Next(1, limit + 1);
            }
        }

        private bool checkSame(int [] a, int c) {
            for (int i = 0; i < c; i++) {
                for (int j = i + 1; j < c; j++) {
                    if (a[i] == a[j]) {
                        return true;
                    }
                }
            }
            return false;
        }

        private void sort(int[] a, int c) {
            int t;
            for (int i = 0; i < c; i++)
            {
                for (int j = i + 1; j < c; j++)
                {
                    if (a[i] > a[j])
                    {
                        t = a[i];
                        a[i] = a[j];
                        a[j] = t;
                    }
                }
            }            
        }
    }
}

 


版权声明:本文为weixin_39410618原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。