标题:我弄了随机抽号,但算法不严谨,会出现重复,求帮助
只看楼主
小庄的故事
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-6
结帖率:0
已结贴  问题点数:20 回复次数:1 
我弄了随机抽号,但算法不严谨,会出现重复,求帮助
using
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int i = 0, j = 0, sign;
        string[] a, b;
        private void xiaocao()
        {
            string strsread;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                timer1.Stop();
                StreamReader file = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                strsread = file.ReadLine();
                if (strsread == null)
                {
                    MessageBox.Show("请选择一个内容非空的文件");
                    xiaocao();
                }
                while (strsread != null)
                {
                    j++;
                    strsread = file.ReadLine();
                }
                a = new string[j];
                b = new string[j];
                StreamReader file2 = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                strsread = file2.ReadLine();
                while (strsread != null)
                {
                    sign = strsread.IndexOf(":");
                    a[i] = strsread.Substring(0,sign);
                    b[i] = strsread.Substring(sign);
                    i++;
                    strsread = file2.ReadLine();
                }
                file.Dispose();
                file2.Dispose();
            }
            else MessageBox.Show("请选择一个文件");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            xiaocao();

        }
        Random ranNum = new Random();
        int intNum = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            intNum = ranNum.Next(0, j);
            label2.Text = a[intNum];

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (j > 0)
            {
                timer1.Start();
            }
            else
            {
                timer1.Dispose();
                MessageBox.Show("已无东西可抽取");
                label2.Text = "";
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            MessageBox.Show(a[intNum] + " " + b[intNum]);
            for (int k = 1; k<= j-1; k++)
            {
                string c  = a[intNum];
                a[intNum] = a[intNum-1];
                a[intNum-1] = c;
            }
            j--;//是不是红色这里的问题呢?


        }
        private Size beforeResizeSize = Size.Empty;

        protected override void OnResizeBegin(EventArgs e)
        {
            base.OnResizeBegin(e);
            beforeResizeSize = this.Size;
        }



        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);
            //窗口resize之后的大小
            Size endResizeSize = this.Size;
            //获得变化比例
            float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;
            float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;

            foreach (Control control in this.Controls)
            {
                if (control is DataGridView)
                    continue;
                //按比例改变控件大小
                control.Width = (int)(control.Width * percentWidth);
                control.Height = (int)(control.Height * percentHeight);
                //为了不使控件之间覆盖 位置也要按比例变化
                control.Left = (int)(control.Left * percentWidth);
                control.Top = (int)(control.Top * percentHeight);
            }
        }
        
    }
}
搜索更多相关主题的帖子: private public 
2014-05-06 21:44
神奈创
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:67
专家分:191
注 册:2013-3-31
得分:20 
建议楼主将随机生成的数字放到hashset,里面会自动帮你处理重复问题
2014-05-07 18:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431110-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.544277 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved