引发了“System.IndexOutOfRangeException”类型的异常(在线等)

ZH408482684 2010-05-19 10:54:18
源码
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;
namespace 猜数字游戏系统
{
public partial class formYX : Form
{
private Random r1;
private int number=0;
private string []s = new string [9];


private int results = 0 ;
public formYX()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
r1 = new Random();
this.button10.Enabled = false;

this.textBox1.Hide();
this.textBox2.Hide();
this.textBox3.Hide();
this.textBox4.Hide();
this.textBox5.Hide();
this.textBox6.Hide();
this.textBox7.Hide();
this.textBox8.Hide();
this.textBox9.Hide();

this.button1.Hide();
this.button2.Hide();
this.button3.Hide();
this.button4.Hide();
this.button5.Hide();
this.button6.Hide();
this.button7.Hide();
this.button8.Hide();
this.button9.Hide();
this.button10.Hide();
}

private void button_B_Click(object sender, EventArgs e)
{
this.button_B.Hide();
this.button_认输.Hide();

timer2.Enabled = true;
timer2.Interval = 1;
// System.Threading.Thread.Sleep(1000);



//this.button2_逃跑.Hide();

this.timer1.Enabled = true;
this.timer1.Interval = 1000;

s[0] = this.button1.Text = random(1).ToString();
s[1] = this.button2.Text = random(1).ToString();
s[2] = this.button3.Text = random(1).ToString();
s[3] = this.button4.Text = random(1).ToString();
s[4] = this.button5.Text = random(1).ToString();
s[5] = this.button6.Text = random(1).ToString();
s[6] = this.button7.Text = random(1).ToString();
s[7] = this.button8.Text = random(1).ToString();
s[8] = this.button9.Text = random(1).ToString();
this.button10.Text = "10";
this.label1.Text = "游戏简介 请在一定时间内记住下列数字";
}
private int random(int difficulty)
{
return r1.Next(difficulty * 10);
}
private void timer1_Tick(object sender, EventArgs e)
{
string s = this.button10.Text ;
int x = Convert.ToInt32(s);

if (x == 0)
{
//this.timer1.Enabled = false;
this.button1.Hide();
this.button2.Hide();
this.button3.Hide();
this.button4.Hide();
this.button5.Hide();
this.button6.Hide();
this.button7.Hide();
this.button8.Hide();
this.button9.Hide();
//this.button10.Hide();

this.textBox1.Show();
this.textBox2.Show();
this.textBox3.Show();
this.textBox4.Show();
this.textBox5.Show();
this.textBox6.Show();
this.textBox7.Show();
this.textBox8.Show();
this.textBox9.Show();


this.button10.Text = "10";
number++;
if (number == 1)
{
this.label1.Text = "你有十秒钟时间 请将数字依次填入";
}
if (number == 2)
{
this.label1.Text = "时间到!!!";
this.timer1.Enabled = false;

this.textBox1.Enabled = false;
this.textBox2.Enabled = false;
this.textBox3.Enabled = false;
this.textBox4.Enabled = false;
this.textBox5.Enabled = false;
this.textBox6.Enabled = false;
this.textBox7.Enabled = false;
this.textBox8.Enabled = false;
this.textBox9.Enabled = false;
int i=0;
for (i=0;i<9;i++)
{
switch (i)
{
case 0:
if (s[0].ToString() == this.textBox1.Text.ToString())
{
this.results++;
}
break;
case 1:
if (s[1].ToString() == this.textBox2.Text.ToString())
{
this.results++;
}
break;
case 2:
if (s[2].ToString() == this.textBox3.Text.ToString())
{
this.results++;
}
break;
case 3:
if (s[3].ToString() == this.textBox4.Text.ToString())
{
this.results++;
}
break;
case 4:
if (s[4].ToString() == this.textBox5.Text.ToString())
{
this.results++;
}
break;
case 5:
if (s[5].ToString() == this.textBox6.Text.ToString())
{
this.results++;
}
break;
case 6:
if (s[6].ToString() == this.textBox7.Text.ToString())
{
this.results++;
}
break;
case 7:
if (s[7].ToString() == this.textBox8.Text.ToString())
{
this.results++;
}
break;
case 8:
if (s[8].ToString() == this.textBox9.Text.ToString())
{
this.results++;
}
break;
default:
break;
}
}

string tmps = results.ToString();

MessageBox.Show("你的成绩为 "+tmps);
this.button10.Text = "重新开始";
this.button10.Enabled = true;
number = 0;

}
return;
}
x--;

s = x.ToString();
this.button10.Text = s;
}

private void button10_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.timer1.Interval = 1000;

this.button1.Text = random(1).ToString();
this.button2.Text = random(1).ToString();
this.button3.Text = random(1).ToString();
this.button4.Text = random(1).ToString();
this.button5.Text = random(1).ToString();
this.button6.Text = random(1).ToString();
this.button7.Text = random(1).ToString();
this.button8.Text = random(1).ToString();
this.button9.Text = random(1).ToString();
this.button10.Text = "10";
this.label1.Text = "游戏简介 请在一定时间内记住下列数字";

this.textBox1.Hide();
this.textBox2.Hide();
this.textBox3.Hide();
this.textBox4.Hide();
this.textBox5.Hide();
this.textBox6.Hide();
this.textBox7.Hide();
this.textBox8.Hide();
this.textBox9.Hide();

this.button1.Show();
this.button2.Show();
this.button3.Show();
this.button4.Show();
this.button5.Show();
this.button6.Show();
this.button7.Show();
this.button8.Show();
this.button9.Show();
this.button10.Show();
}


}
}
问题描述: 我在调试的时候对s[9] 数组赋值时候 发现赋值成功 但是在number=2的时候 对S[9]与textbox进行比较时候发现s[]引出异常。

PS 我是新手 麻烦大家讲详细点嘛 谢谢
...全文
775 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,082

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧