日期:2014-05-18  浏览次数:20872 次

循环输出出了很神奇的问题
public class Student { public string name; public char sex; public Student(string na, char sx) { name = na; sex = sx; } }

  public class Grade {
  Student[] someone = new Student[50];
  public Student this[int index] { set { if (!(index < 0 || index > someone.Length)) someone[index] = value; } }
  public string answer(int I) { string result = "gh"; for (int a = 0; a <= I; a++) { result += "姓名:" + someone[a].name + "\t\t" + "性别:" + someone[a].sex + "\n"; } return result; }

  }
  Grade G = new Grade(); int i;
  private void button1_Click(object sender, EventArgs e)
  {
  G[i] = new Student(t1.Text, Convert.ToChar(c1.Text));
  i++;
  label4.Text = "班级报道了" + i + "个学生!";


  }

  private void button2_Click(object sender, EventArgs e)
  {
  label3.Text = G.answer( i);
  }

输入了几个学生信息后,按button2,他说方法answer里
result += "姓名:" + someone[a].name + "\t\t" + "性别:" + someone[a].sex + "\n"; } return result;没有未将对象引用设置到对象的实例,但是把变量a改为定量1或者2,它却能够输出,为什么????


------解决方案--------------------
"; for (int a = 0; a <= I;改成小于