简单的成绩排名系统


//这个程序是我自己想的,很简单,只能录入单科成绩并排名,处理了几个输入异常时情况。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PaiMing

{
    public partial class Form1 : Form
    {
        private int p = 0;                  //p起标记作用,很重要
        public Form1()
        {
            InitializeComponent();
        }
        private void zengjia_Click(object sender, EventArgs e)
        {
            int isInt=-1;
            if (xingming.Text.Length > 4||xingming.Text==""){ 
                MessageBox.Show("姓名不能为空且最大长度为4,请重新输入"); 
                return; 
            }
            try {
                isInt = Convert.ToInt32(chengji.Text);
            }
            catch {
                MessageBox.Show("成绩输入错误,必须为数字"); return;
            }
            if (isInt < 0 || isInt > 100){
                MessageBox.Show("成绩输入错误,在数字1~100之间");
                return; 
            }
            if (textBox1.Lines.Length == 0 || p != 0){ 
                textBox1.AppendText(xingming.Text + "  :  " + chengji.Text); 
                p = 0; 
            }
            else textBox1.AppendText("\r\n" + xingming.Text + "  :  " + chengji.Text);
        }
        private void qiufen_Click(object sender, EventArgs e)
        {
            if (p > 0) { 
                MessageBox.Show("已经排好序了!"); 
                return; 
            }
            if (textBox1.Text == "") { 
                MessageBox.Show("请先增加成绩记录!"); 
                return; 
            }
            else{
                p++;
                int i = 0;
                string[] lines = textBox1.Lines;
                int l = textBox1.Lines.Length;
                string maxName = "";
                int maxScore = -1;
                string[] shuzu = new string[l];
                int[] paixu = new int[l];
                foreach (string line in lines){
                    shuzu[i] = line;
                    string[] s = line.Split(':');
                    string name = s[0];
                    string strScore = s[1];
                    int score = Convert.ToInt32(strScore);
                    paixu[i++] = score;
                    if (score >= maxScore) 
                    { maxName = name; maxScore = score; }
                }
                string stemp="";
                int temp=0;
                for (int j = 0; j < l-1; j++){
                    for (int k = 0; k < l - j-1; k++){
                        if (paixu[k] > paixu[k + 1]){
                            temp = paixu[k];
                            paixu[k] = paixu[k + 1];
                            paixu[k + 1] = temp;
                            stemp = shuzu[k]; 
                            shuzu[k] = shuzu[k + 1]; 
                            shuzu[k + 1] = stemp;
                        }
                    }
                }
                textBox1.Text = "";
                int mingci = 1;
                textBox2.Text = "";
                for (int m = l - 1; m >= 0; m--){
                    textBox1.AppendText(shuzu[m] + "\r\n");
                    textBox2.AppendText(mingci++ + "\r\n");
                }
                MessageBox.Show(string.Format("{0}是第一名,成绩为{1}", maxName, maxScore));
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            p = 0;
            textBox1.Text = "";
            textBox2.Text = "";
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值