using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MySelectNum
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox4;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.textBox4 = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(216, 96);
   this.textBox1.Name = "textBox1";
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(216, 144);
   this.textBox2.Name = "textBox2";
   this.textBox2.TabIndex = 1;
   this.textBox2.Text = "";
   //
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(216, 192);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(96, 21);
   this.textBox3.TabIndex = 2;
   this.textBox3.Text = "";
   //
   // label1
   //
   this.label1.AutoSize = true;
   this.label1.Location = new System.Drawing.Point(136, 192);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(66, 17);
   this.label1.TabIndex = 3;
   this.label1.Text = "随机数个数";
   //
   // label2
   //
   this.label2.AutoSize = true;
   this.label2.Location = new System.Drawing.Point(136, 96);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(66, 17);
   this.label2.TabIndex = 3;
   this.label2.Text = "随机数下限";
   //
   // label3
   //
   this.label3.AutoSize = true;
   this.label3.Location = new System.Drawing.Point(136, 144);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(66, 17);
   this.label3.TabIndex = 3;
   this.label3.Text = "随机数上限";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(80, 232);
   this.button1.Name = "button1";
   this.button1.TabIndex = 4;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox4
   //
   this.textBox4.Location = new System.Drawing.Point(32, 280);
   this.textBox4.Multiline = true;
   this.textBox4.Name = "textBox4";
   this.textBox4.Size = new System.Drawing.Size(448, 56);
   this.textBox4.TabIndex = 2;
   this.textBox4.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(504, 355);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.textBox4);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   int a=0;
   int min=0;
   int max=0;
   if(this.textBox3.Text!="" && this.textBox2.Text!="" && this.textBox1.Text!="")
   {
    a=int.Parse(this.textBox3.Text.ToString());
    min=int.Parse(this.textBox1.Text.ToString());
    max=int.Parse(this.textBox2.Text.ToString());
    int[] b=new int[a];
    string str1="";
    string str="";
    b=GetRandomArray(a,min,max);
    for(int i=0;i<a;i++)
    {
     str1=b[i].ToString();
     str=","+str1;


    }
    MessageBox.Show(str);
   }
   

  
  }


  /// <summary>
  /// 功能描述:返回不重复随机数数组
  /// 作者:    杨艳峰
  /// 创建日期:2006-4-30
  /// </summary>
  /// <param name="Num">随机数个数</param>
  /// <param name="minNum">随机数下限</param>
  /// <param name="maxNum">随机数上限</param>
  /// <returns></returns>
  public int[] GetRandomArray(int Number,int minNum,int maxNum)
  {
   int j;
   int[] b=new int[Number];
   Random r=new Random();
   for(j=0;j<Number;j++)
   {
    int i=r.Next(minNum,maxNum+1);
    int num=0;
    for(int k=0;k<j;k++)
    {
     if(b[k]==i)
     {
      num=num+1;
     }
    }
    if(num==0 )
    {
     b[j]=i;
    }
    else
    {
     j=j-1;
    }
   }
   return b;
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   int a=0;
   int min=0;
   int max=0;
   if(this.textBox3.Text!="" && this.textBox2.Text!="" && this.textBox1.Text!="")
   {
    a=int.Parse(this.textBox3.Text.ToString());
    min=int.Parse(this.textBox1.Text.ToString());
    max=int.Parse(this.textBox2.Text.ToString());
    int[] b=new int[a];
    string str="";
    string str1="";
    int tmp=0;
    b=GetRandomArray(a,min,max);
    for(int k=0;k<a;k++)
    {
     for(int j=0;j<a-1;j++)
     {
      if(b[j]>b[j+1])
      {
       tmp=b[j];
       b[j]=b[j+1];
       b[j+1]=tmp;

      }
     }
    }
    for(int i=0;i<a;i++)
    {
     str1=b[i].ToString();
     if(str.Length!=0)
     {
      str=str+","+str1;
     }
     else
     {
      str=str1;
     }


    }
    //MessageBox.Show(str);
    this.textBox4.Text=str;
   }
   
  }

 


 }
}

posted on 2007-02-26 16:51  Yang-S  阅读(197)  评论(0)    收藏  举报