请问一个显示随机数的问题
各位,我想按一下button后就在textbox上显示出一个9位数的数字
要求规律如下:
数字在0-9中取值
第1,3,9位数为奇数
第2,4,5,7位为偶数
第6位数等于第8位数,可以为奇数也可以为偶数。
我写了以下这些代码,显示出来的数字是符合以上规律的。
但是我发现显示出来的数字大部分是相同的。想请教一下代码的问题到底出在哪里了
谢谢!!!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
public int a1 = 0;
public int a2 = 0;
public int a3 = 0;
public int a4 = 0;
public int a5 = 0;
public int a6 = 0;
public int a7 = 0;
public int a8 = 0;
public int a9 = 0;
/// <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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
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)
{
}
//判断是否为奇数
private bool panduan(int i)
{
if(i%2 == 0)
{
return false;
}
else
{
return true;
}
}
//生成一个奇数
private void jishu(ref int a)
{
for(int i=1;i<=100;i++)
{
Random r = new Random();
int j = r.Next(0,10);
if(panduan(j))
{
a = j;
break;
}
}
}
//生成一个偶数
private void oushu(ref int b)
{
for(int k=1;k<=100;k++)
{
Random r = new Random();
int l = r.Next(0,10);
if(!panduan(l))
{
b = l;
break;
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
StringBuilder shu = new StringBuilder("");
jishu(ref a1);
oushu(ref a2);
jishu(ref a3);
oushu(ref a4);
oushu(ref a5);
oushu(ref a7);
jishu(ref a9);
Random r = new Random();
a6 = r.Next(0,10);
a8 = a6;
shu.Append(a1.ToString());
shu.Append(a2.ToString());
shu.Append(a3.ToString());
shu.Append(a4.ToString());
shu.Append(a5.ToString());
shu.Append(a6.ToString());
shu.Append(a7.ToString());
shu.Append(a8.ToString());
shu.Append(a9.ToString());
textBox1.Text = shu.ToString();
}
}
}