C#越界问题
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Exam
{
public partial class Form1 : Form
{
private string [] Questions={"被誉为“诗史”的唐代著名诗人是( )",
"《春江花月夜》是( )的作品",
"唐代诗人被称为“鬼才”的是( )",
"开创我国田园诗新领域的诗人是( )",
"《无题诗》是( )的代表作"};
private string[,] Options ={{"李白","杜甫","白居易","王维"},
{"王维","岑参","张若曦","李贺"},
{"孟郊","李煜","李商隐","李贺"},
{"李商隐","苏轼","欧阳修","韩愈"}};
private int[] UserChoices ={ -1, -1, -1, -1, -1 };
private int QuestionID = 0;
private void ReadQuestion(int qid)
{
lblID .Text =(qid +1)+"/"+Questions .Length ;
lblQuestion .Text =Questions[qid];
rbOption1 .Text =Options[qid,0];
rbOption2 .Text =Options[qid,1];
rbOption3 .Text =Options[qid ,2];
rbOption4 .Text =Options [qid ,3];
if (UserChoices [qid]==0){
rbOption1 .Checked =true ;
}
else if (UserChoices [qid ]==1)
{
rbOption2 .Checked =true ;
}
else if (UserChoices [qid ]==2)
{
rbOption3 .Checked =true ;
}
else if (UserChoices [qid ]==3)
{
rbOption4 .Checked =true ;
}
else
{
rbOption1 .Checked =false ;
rbOption2 .Checked =false ;
rbOption3 .Checked =false ;
rbOption4 .Checked =false ;
}
if (qid==0)
butBack .Enabled =false ;
else if (qid ==Questions .Length -1)
butNext .Enabled =false ;
else
{
butBack .Enabled =true ;
butNext .Enabled =true ;
}
}
public Form1()
{
InitializeComponent();
}
private void butNext_Click(object sender, EventArgs e)
{
QuestionID++;
ReadQuestion(QuestionID);
}
private void butBack_Click(object sender, EventArgs e)
{
QuestionID --;
ReadQuestion(QuestionID);
}
private void butExit_Click(object sender, EventArgs e)
{
this .Close ();
}
private void rbOption1_CheckedChanged(object sender, EventArgs e)
{
UserChoices[QuestionID] = 0;
}
private void rbOption2_CheckedChanged(object sender, EventArgs e)
{
UserChoices[QuestionID] = 1;
}
private void rbOption3_CheckedChanged(object sender, EventArgs e)
{
UserChoices[QuestionID] = 2;
}
private void rbOption4_CheckedChanged(object sender, EventArgs e)
{
UserChoices[QuestionID] = 3;
}
private void Form1_Load(object sender, EventArgs e)
{
ReadQuestion(0);
}
}
}
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.IndexOutOfRangeException: 索引超出了数组界限。
在 Exam.Form1.ReadQuestion(Int32 qid) 位置 C:\Program Files\QQ\Users\970719295\FileRecv\Exam\Exam\Form1.cs:行号 28
在 Exam.Form1.butNext_Click(Object sender, EventArgs e) 位置 C:\Program Files\QQ\Users\970719295\FileRecv\Exam\Exam\Form1.cs:行号 71
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.42 (RTM.050727-4200)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Exam
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///C:/Program%20Files/QQ/Users/970719295/FileRecv/Exam/Exam/bin/Debug/Exam.exe
----------------------------------------
System.Windows.Forms
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.42 (RTM.050727-4200)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
---------------------------------------- --------------------编程问答-------------------- 应该是数组越界,自己设断点调试一下吧。
补充:.NET技术 , C#