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

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;
                    }
                }
            }            
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值