//问题描述
//有时间正确
有时间取不到随即数 有时间数组索引超出范围
竟然能取到z
我实在看不出来
请高手帮助
//
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
//动态数组命名空间
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
yanzheng();
Response.Write("<meta http-equiv='refresh' content='30;URL=Default.aspx' /> ");
//30秒后自动刷新页面
}
protected void tijiao_Click(object sender, EventArgs e)
{
try
{
if (this.shuru.Text == "")
{
Response.Write("<script language=javascript>window.alert('请输入验证码!')</script>");
}
else
{
if (this.shuru.Text == Session["yzm"].ToString())
{
this.jieguo.Text = "你终于成功了";
}
else
{
this.jieguo.Text = "你失败了";
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
//通过二维数组产生验证码
public void yanzheng()
{
string[,] myArray;
// 创建数组
myArray = new string[,] { {"Z","A","B","C","D","E","F","G","H","I"},
//填充数组
{"A","11","32","33","54","55","76","77","98","99"},
{"B","12","31","34","53","56","75","78","97","01"},
{"C","13","30","35","52","57","74","79","96","02"},
{"D","14","29","36","51","58","73","80","95","03"},
{"E","15","28","37","50","59","72","81","94","04"},
{"F","16","27","38","49","60","71","82","93","05"},
{"G","17","26","39","48","61","70","83","92","06"},
{"H","18","25","40","47","62","69","84","91","07"},
{"I","19","24","41","46","63","68","85","90","08"},
{"J","20","23","42","45","64","67","86","89","09"},
{"K","21","22","43","44","65","66","87","88","10"}};
try
{
for (int x = 0; x < 12; x++)
//输出数组
(方便测试)
{
for (int y = 0; y < 10; y++)
{
Response.Write(myArray[x, y] + " ");
}
Response.Write("<br>");
}
//输出字母索引行
for (int j = 0; j < 10; j++)
{
Response.Write("列:" + myArray[0, j] + " ");
}
//字输出母索引列
Response.Write("<br>");
for (int j = 0; j < 12; j++)
{
Response.Write("行:" + myArray[j, 0] + " ");
}
Random rdm = new Random();
// 实例化一个生成器
for (int x = 0; x < 12; x++)
{
for (int y = 0; y < 10; y++)
{
if (myArray[x, y] == myArray[rdm.Next(1, 12), rdm.Next(1, 10)])
{
Response.Write(myArray[x, 0]);
//测试输出用
Response.Write(myArray[0, y]);
Response.Write("<br>");
Response.Write(x);
Response.Write(y);
Response.Write("<br>");
Response.Write(myArray[y, x]);
Session["lie"] = myArray[x, 0];
// 产生行字母
Session["hang"] = myArray[0, y]; //产生列字母
Session["yzm"] = myArray[y, x];
//产生行和列坐标交叉点的数字
}
}
Response.Write("<br>");
}
Response.Write("<br>");
this.xianshi.Text = Session["lie"].ToString();
this.xianshi.Text += Session["hang"].ToString();
//给显示验证码的label赋值
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}