using System;using System.Windows.Forms;namespace StudentAgeCalculator{ public partial class MainForm : Form { private Student[] students = new Student[5]; // 创建一个包含5个学生对象的数组 public MainForm() { InitializeComponent(); // 初始化5个学生对象 students[0] = new Student("张三", "001", 18); students[1] = new Student("李四", "002", 19); students[2] = new Student("王五", "003", 20); students[3] = new Student("赵六", "004", 21); students[4] = new Student("钱七", "005", 22); } private void btnCalculate_Click(object sender, EventArgs e) { int totalAge = 0; foreach (Student student in students) // 遍历学生数组,计算总年龄 { totalAge += student.Age; } double averageAge = (double)totalAge / students.Length; // 计算平均年龄 MessageBox.Show($"5个学生的平均年龄为:{averageAge:F2}岁"); // 显示平均年龄 } } public class Student { public string Name { get; set; } public string Number { get; set; } public int Age { get; set; } public Student(string name, string number, int age) { Name = name; Number = number; Age = age; } }}所有注释
时间: 2023-12-06 14:43:47 浏览: 73
都是用来解释代码的作用和功能的,这段代码是一个 C# 程序。首先定义了一个 MainForm 类,继承自 Form 类,表示一个窗体。在 MainForm 类中定义了一个包含 5 个 Student 对象的数组,每个 Student 对象包含姓名、学号和年龄三个属性。然后在 MainForm 构造函数中初始化了这 5 个学生对象。程序还定义了一个 btnCalculate_Click 方法,用来计算这 5 个学生的平均年龄并显示出来。其中 foreach 循环遍历了学生数组,计算总年龄,再根据数组长度计算出平均年龄并显示在 MessageBox 中。
相关问题
讲代码补充完整using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace one { public partial class Form1 : Form { private Classes mycla = new Classes(); public Form1() { InitializeComponent(); } private void List() { // Student s; textBox3.Text = ""; for (int i = 1; i <=mycla.Count; i++) { //编写代码 //.... // textBox3.Text += s.code + "\t"+s.Name+"\r\n"; } } private void button1_Click(object sender, EventArgs e) { //编写程序 List();//显示学生信息 } private void button2_Click(object sender, EventArgs e) { //编写代码 } } }
以上是一个Windows Forms应用程序的部分代码,包含一个窗体类Form1和一个Classes类对象mycla。下面是将其使用的代码补充完整:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace one
{
public partial class Form1 : Form
{
private Classes mycla = new Classes();
public Form1()
{
InitializeComponent();
}
private void List()
{
textBox3.Text = "";
for (int i = 1; i <= mycla.Count; i++)
{
Student s = mycla.GetStudent(i);
textBox3.Text += s.code + "\t" + s.Name + "\r\n";
}
}
private void button1_Click(object sender, EventArgs e)
{
List();
}
private void button2_Click(object sender, EventArgs e)
{
string code = textBox1.Text;
string name = textBox2.Text;
Student s = new Student(code, name);
mycla.AddStudent(s);
textBox1.Text = "";
textBox2.Text = "";
List();
}
}
}
在List方法中,我们使用for循环遍历mycla对象中所有的学生信息,并且将每个学生的学号和姓名信息打印在textBox3中。
在button2_Click方法中,我们通过textBox1和textBox2获取用户输入的学号和姓名信息,并且创建一个新的学生对象s,将其添加到mycla对象中。最后清空textBox1和textBox2,并且调用List方法刷新显示。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormsApp16 { public class student { private int stuno; private string stuname; private int stuage; private string stusex; private string stuspeciality; public student(int myno ,string myname, int myage ,string mysex,string myspecility) { this.Stuno = myno; this.Stuname = myname; this.Stuage = myage; this.Stusex = mysex; this.Stuspeciality = myspecility; } public int Stuno { get => stuno; } public string Stuname { get => stuname; } public string Stusex { get => stusex; } public string Stuspeciality { get => stuspeciality; set => stuspeciality = value; } public int Stuage { get => stuage; set => stuage = value; } } }把这段代码优化成输入信息后再只读
这段代码是 C# 语言的代码,可以通过在 Windows 系统上安装 Visual Studio 编辑器进行修改和运行。
以下是将该代码优化为输入信息后再只读的代码示例:
```csharp
using System;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class Form1 : Form
{
private int stuno;
private string stuname;
private int stuage;
private string stusex;
private string stuspeciality;
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
stuno = int.Parse(txtStuno.Text);
stuname = txtStuname.Text;
stuage = int.Parse(txtStuage.Text);
stusex = txtStusex.Text;
stuspeciality = txtStuspeciality.Text;
// 显示信息
txtInfo.Text = $"学号:{stuno}\r\n姓名:{stuname}\r\n年龄:{stuage}\r\n性别:{stusex}\r\n专业:{stuspeciality}";
// 设置文本框只读
txtStuno.ReadOnly = true;
txtStuname.ReadOnly = true;
txtStuage.ReadOnly = true;
txtStusex.ReadOnly = true;
txtStuspeciality.ReadOnly = true;
}
}
}
```
这段代码实现了一个简单的 Windows 窗体应用程序,包含了一个输入学生信息的表单,用户在填写完信息后,点击提交按钮,会将学生信息显示在一个文本框中,并将输入框设置为只读状态,不允许再次编辑。
阅读全文