C#——设计一个窗体程序,实现功能: (1)输入学生姓名和考试成绩并保存到结构体数组中 (2)使用foreach语句求最高分并输出对应的姓名。

设计如下界面:

编写代码:

 

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 c

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        Student []stu=new Student[100];

        int num = 0;

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                stu[num].name = textBox1.Text;

                stu[num].grade = Convert.ToDouble(textBox2.Text);

                num++;

                label3.Text = "成功添加" + num + "条记录";

                textBox1.Text = "";

                textBox2.Text = "";

            }

            catch 

            {

                MessageBox.Show("请输入正确的学生信息");

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            double max=stu[0].grade;

            foreach(Student i in stu)

            {

                if(i.grade>max)

                {

                    max = i.grade;

                    label3.Text = "最高分:" +max+ "\n姓名:"+i.name;

                }

            }

            

        }

    }

    struct Student 

    {

        public string name;

        public Double grade;

    }

}

 

运行结果:

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88773361