| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1039 人关注过本帖
标题:求路过的大神解惑???不知道为什么排不了序。。。
只看楼主 加入收藏
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:0 
你这个弄成这样其实这没那必要,你的构造函数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;
            }
        }
    }
}

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-17 22:05
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:0 
我没调试,不过应该是能排序的,至于要按什么排序象我说的那样你就先排好再加进集合里,然后就会得出是你要的按哪个排序的结果了!

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-17 22:08
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:0 
好奇心让我再回收站把它拿了回来调试下,是可以排序的了,不过平均分没有了,我又改了下,不过你想要别的按平均分排序你就自己写方法

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),Convert.ToSingle(stu.Averger));
                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,float avg)
            {
                this.name = name;
                this.number = number;
                this.score1 = score1;
                this.score2 = score2;
                this.Averger = avg;
            }
            public int CompareTo(object num)
            {
                student otherStudent = num as student;
                int result = this.(otherStudent.number);
                return result;
            }
        }
    }
}
我又删了,这次别告诉我还不行···

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-17 22:13
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:0 
谢谢你了。。。

你不勇敢,谁替你坚强。。。
2013-06-17 22:28
快速回复:求路过的大神解惑???不知道为什么排不了序。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039304 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved