你这个弄成这样其实这没那必要,你的构造函数student都有两个你第2个你肯定要用的啊,你这个写的真是不知道怎么说了,我在你的基础上改了下,希望满足你的要求吧
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
ArrayList StuList = new ArrayList();
student stu = new student();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "学生信息情况" + "\r\n" + "姓名" + "\t\t" + "学号" + "\t\t" + "英语成绩" + "\t" + "C#成绩" + "\t\t" + "平均成绩" + "\r\n";
for (int j = 0; j < 2; j++)
{
while (true)
{
stu.name = Microsoft.VisualBasic.Interaction.InputBox("请输入名字", "输入名字", "黄辉", -1, -1);
break;
}
while (true)
{
stu.number = Microsoft.VisualBasic.Interaction.InputBox("请输入学号", "输入学号", "1215080131", -1, -1);
break;
}
while (true)
{
stu.score1 = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("请输入成绩", "输入成绩", "90", -1, -1));
stu.score2 = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("请输入成绩", "输入成绩", "90", -1, -1));
break;
}
stu.Averger = (stu.score1 + stu.score2) / 2;
student st = new student(stu.name.ToString(),stu.number.ToString(),Convert.ToInt32( stu.score1),Convert.ToInt32( stu.score2));
StuList.Add(st);
textBox1.Text = textBox1.Text + stu.name + "\t\t" + stu.number + "\t" + stu.score1.ToString() + "\t\t" + stu.score2 + "\t\t" + stu.Averger + "\r\n";
}
}
private void button2_Click(object sender, EventArgs e)
{
StuList.Sort();
textBox2.Text = "按学号排序" + "\r\n" + "学号" + "\t\t" + "姓名" + "\t\t" + "英语成绩" + "\t" + "C#成绩" + "\t\t" + "平均成绩" + "\r\n"; ;
foreach (student p in StuList)
{
textBox2.Text = textBox2.Text + p.number + "\t\t" + p.name + "\t" + p.score1.ToString() + "\t\t" + p.score2.ToString() + "\t\t" + p.Averger.ToString() + "\r\n";
}
}
public class student : IComparable
{
public string name;
public string number;
public int score1;
public int score2;
public float Averger = 0;
public student()
{
}
public student(string name, string number, int score1, int score2)
{
this.name = name;
this.number = number;
this.score1 = score1;
this.score2 = score2;
}
public int CompareTo(object num)
{
student otherStudent = num as student;
int result = this.(otherStudent.number);
return result;
}
}
}
}
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
ArrayList StuList = new ArrayList();
student stu = new student();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "学生信息情况" + "\r\n" + "姓名" + "\t\t" + "学号" + "\t\t" + "英语成绩" + "\t" + "C#成绩" + "\t\t" + "平均成绩" + "\r\n";
for (int j = 0; j < 2; j++)
{
while (true)
{
stu.name = Microsoft.VisualBasic.Interaction.InputBox("请输入名字", "输入名字", "黄辉", -1, -1);
break;
}
while (true)
{
stu.number = Microsoft.VisualBasic.Interaction.InputBox("请输入学号", "输入学号", "1215080131", -1, -1);
break;
}
while (true)
{
stu.score1 = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("请输入成绩", "输入成绩", "90", -1, -1));
stu.score2 = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("请输入成绩", "输入成绩", "90", -1, -1));
break;
}
stu.Averger = (stu.score1 + stu.score2) / 2;
student st = new student(stu.name.ToString(),stu.number.ToString(),Convert.ToInt32( stu.score1),Convert.ToInt32( stu.score2));
StuList.Add(st);
textBox1.Text = textBox1.Text + stu.name + "\t\t" + stu.number + "\t" + stu.score1.ToString() + "\t\t" + stu.score2 + "\t\t" + stu.Averger + "\r\n";
}
}
private void button2_Click(object sender, EventArgs e)
{
StuList.Sort();
textBox2.Text = "按学号排序" + "\r\n" + "学号" + "\t\t" + "姓名" + "\t\t" + "英语成绩" + "\t" + "C#成绩" + "\t\t" + "平均成绩" + "\r\n"; ;
foreach (student p in StuList)
{
textBox2.Text = textBox2.Text + p.number + "\t\t" + p.name + "\t" + p.score1.ToString() + "\t\t" + p.score2.ToString() + "\t\t" + p.Averger.ToString() + "\r\n";
}
}
public class student : IComparable
{
public string name;
public string number;
public int score1;
public int score2;
public float Averger = 0;
public student()
{
}
public student(string name, string number, int score1, int score2)
{
this.name = name;
this.number = number;
this.score1 = score1;
this.score2 = score2;
}
public int CompareTo(object num)
{
student otherStudent = num as student;
int result = this.(otherStudent.number);
return result;
}
}
}
}
慢慢前进走,不求一步登天,只求慢慢前进