namespace 学生信息显示升级版
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //设置这些静态变量是为了充当全局变量
        static int z=0;
        static int a = 0;
        static int b= 0;
        static int c = 0;
        public  student[]st=new student[z];
        static int i = 0;
        static int two=a;
        static int tree=c;
        public class student
        {
            private string name;
            private string sex;
            private string banji;
            private string jiguan;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public string Sex
            {
                get
                {
                    return sex;
                }
                set
                {
                    sex = value;
                }
            }
            public string  Banji
            {
                get
                {
                    return banji;
                }
                set
                {
                    banji = value;
                }
            }
            public string Jiguan
            {
                get
                {
                    return jiguan;
                }
                set
                {
                    jiguan = value;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label5.Text = "1班的学生人数是:"+textBox1.Text + "\n" + "2班的学生人数是:"+textBox2.Text + "\n" + "3班的学生人数是:"+textBox3.Text;
            a = int.Parse(textBox1.Text);
            b = int.Parse(textBox2.Text);
            c = int.Parse(textBox3.Text);
            z = a + b + c;
            label10.Text = a + "\n" + b + "\n" + c + "\n" + z;//这句是为了检验a,b,c,z值有没有改变
        }        private void button2_Click(object sender, EventArgs e)
        {
            
            if (xuanzhe.SelectedIndex != -1)
            {
                tabs.SelectedTab = tab2;
                if (xuanzhe.SelectedIndex == 0)
                {
                    label6.Text = "1班人数为:" + a;
                }
                else
                {
                    if (xuanzhe.SelectedIndex == 1)
                    {
                        label6.Text = "2班人数为:" + b;
                    }
                    else
                    {
                        label6.Text = "3班人数为:" + c;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择第几班进行输入!");
            }        }        private void label5_Click(object sender, EventArgs e)
        {
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (xuanzhe.SelectedIndex == 0)
            {
                student at1 = new student();
                at1.Name = textBox4.Text;
                at1.Sex = textBox5.Text;
                at1.Jiguan = textBox6.Text;
                if (i < a)
                {
                    st[i++] = at1;//问题就在这里了,我一点录入就会显示索引超过数组界限
                    textBox4.Clear();
                    textBox5.Clear();
                    textBox6.Clear();
                    textBox4.Focus();
                }
                else
                {
                    MessageBox.Show("1班学生信息已全部输入完毕!");
                }
            }
            else
            {
                if (xuanzhe.SelectedIndex == 1)
                {
                    student at1 = new student();
                    at1.Name = textBox4.Text;
                    at1.Sex = textBox5.Text;
                    at1.Jiguan = textBox6.Text;
                    if (two < a + b)//问题就在这里了,我一点录入信息就会显示索引超过数组界限
                    {
                        st[two++] = at1;
                    }
                    else
                    {
                        MessageBox.Show("2班学生信息已全部输入完毕!");
                    }
                }
                else
                {
                    student at1 = new student();
                    at1.Name = textBox4.Text;
                    at1.Sex = textBox5.Text;
                    at1.Jiguan = textBox6.Text;
                    if (tree < a + b + c)//问题就在这里了,我一点录入信息就会显示索引超过数组界限
                    {
                        st[two++] = at1;
                    }
                    else
                    {
                        MessageBox.Show("3班学生信息已全部输入完毕!");
                    }
                }            }
        }       
    }
}
我是想弄一个信息显示程序,用选项卡弄了3页,首页先输入各班人数,然后用listview 选择要输入那班信息,然后就跳到第二页,进行输入,可是输入信息一想录入就会弹出索引 超过数组界限,不懂啊

解决方案 »

  1.   

    你ST没这么多元素.你i++超过最大了.
      

  2.   


    if (tree < a + b + c)//问题就在这里了,我一点录入信息就会显示索引超过数组界限
                        {
                            st[two++] = at1;
                        }单步跟踪一下,有惊喜哦。
      

  3.   

    a代表1班人数,b代表2班人数,c代表3班人数,z是3班的总人数,我设定z=a+b+c;而i=0;st[i++]怎么会超过数组上限,我原本以为是设定的静态变量没有起到作用,a,b,c,z还是都等于0。所以我才写了一条label10=a+"\n"+b+"\n"+c+"\n"+z;检验,结果,证实,a,b,c,z都已经被重新赋值拉,这才是我最想不懂的地方
      

  4.   

    你的命名 中英文结合啊, Sex ,Banji 我在想为什么这样呢? 要不全英文,要不全汉子拼音,这样的真不好。
      

  5.   

    把st 和 i 的值都输出来看看,
      

  6.   

    z = a + b + c;
    st=new student[z];
    这样顺序写才对,不然ST[0]当然超过了.
      

  7.   


     if (two < a + b)//问题就在这里了,我一点录入信息就会显示索引超过数组界限
    {
    int index=two++ ;
    if (index< st.Length)
                {
     st[index] = at1;
                }
    else
    {
    MessageBox.Show("数据值超过索引最大值!");
    }
                            
    }
      

  8.   

    我不是想从数组取值,是想赋值给数组,而且,我是想根据输入的数的多少,数组就创建多少个元素,比如,输入5,则st[z]等于st[5],而不是已经在代码限定好数组元素个数,你说的,判断一下取的索引值是否为正数 会不会超过这个数组的最大索引。就像这一楼写的这样。只不过换个形式,不会直接输入然后运行错误,而是弹出一个警告框而已。
      

  9.   

    我是想根据输入的数的多少,数组就创建多少个元素,比如,输入5,则st[z]等于st[5],那这个要怎么才能实现?
      

  10.   

    输入之后,在重新分配数组,st=new student[这里是你输入的数字];