用winform做一个简单的成绩录入 列表
输入人数 按button2 创建数组
再输入姓名 性别 年龄 三次作业成绩 一个期末成绩
要求列出 所有人的姓名 性别 年龄 总评成绩
public static void stat()
{
try
{
j = 0;
for (i = 0; i < k1-1; i++)
{
cj[i] = new student();
}
}
catch(IndexOutOfRangeException e1)
{
k1 = 0;
}
这里老报超出数组界限
button事件里输入后报错 无法输出
会不会是因为那几个静态的东西??
麻烦大家帮忙 谢谢using System;
using System.Collections.Generic;
using System.Text;namespace WindowsApplication1
{
public class person
{
public string name;
public char sex;
public int age;
public person(string a, char b, int c)
{
name = a;
sex = b;
age = c;
}
public person()
{
}
}
public class student:person
{
public double a1, a2, a3, b1,c1;
public student(double a, double b, double c, double d, string e, char f, int g)
: base(e, f, g)
{
a1 = a;
a2 = b;
a3 = c;
b1 = d;
}
public student()
: base()
{
} public double zong()
{
c1 = (a1 + a2 + a3) / 3 * 0.3 + b1 * 0.7;
return c1;
}
}
public class sta
{
static public int k1,i,j;
static public student[] cj = new student[k1];
public static void stat()
{
try
{
j = 0;
for (i = 0; i < k1-1; i++)
{
cj[i] = new student();
}
}
catch(IndexOutOfRangeException e1)
{
k1 = 0;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
string a;
char b;
int c, i1;
double d, e1, f, g;
i1 = sta.j;
if (i1 > sta.k1) { }
else
{
a = Convert.ToString(textBox2.Text);
b = Convert.ToChar(textBox3.Text);
c = Convert.ToInt32(textBox4.Text);
d = Convert.ToDouble(textBox5.Text);
e1 = Convert.ToDouble(textBox6.Text);
f = Convert.ToDouble(textBox7.Text);
g = Convert.ToDouble(textBox8.Text);
sta.cj[i1].name = a;
sta.cj[i1].sex = b;
sta.cj[i1].age = c;
sta.cj[i1].a1 = d;
sta.cj[i1].a2 = e1;
sta.cj[i1].a3 = f;
sta.cj[i1].b1 = g; richTextBox1.Text = sta.cj[i1].name + sta.cj[i1].sex + sta.cj[i1].age + sta.cj[i1].c1;
sta.j++;
}
}
catch (FormatException e2)
{
}
catch (Exception e3)
{
}
} private void button2_Click(object sender, EventArgs e)
{
int a ;
a = Convert.ToInt32(textBox1.Text);
sta.k1 = a;
sta.stat();
richTextBox1.Text=Convert.ToString(sta.k1);
}
}
}
static public student[] cj = new student[k1];
应该是在K1赋值前,cj就已经被静态构造初始化完了,大小已经固定了.
{
static public int k1,i,j;
static public student[] cj = new student[k1];
这个地方有点问题吧,因为你声明K1的时候没有赋初值,所以k1为0,你声明student的时候就把这个student数组的长度初始化为零。
所以你后面一引用这个数组的时候,就会出现下标越界的问题了。