C#——利用静态成员统计人数

本文介绍了一种使用C#编程语言通过静态成员来统计学生人数的方法。在设计的用户界面上,用户可以输入学生信息,程序会自动记录已添加的学生总数,并分别计算男生和女生的数量。当点击按钮时,程序会显示男生和女生的人数以及所有学生名单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计界面:

编写代码

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[5];

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

using System; using System.Data; using System.Text; namespace ZKXP.BLL { /// /// OnLine 的摘要说明。 /// public class OnLine { private string USERNAME; private int OFFLINEDIFF; private int REMOVEDIFF; public OnLine() { //直接在这里设定、或从配置文件中读取配置参数 ///存放用户名的Session名 USERNAME = "UserName"; ///多少分钟不活动的用户从在线列表中删除 OFFLINEDIFF = 5; ///多少秒执行一次删除不活动用户 REMOVEDIFF = 30; if(System.Web.HttpContext.Current.Application["OnlineTalbe"] == null) { this.CashTableInit(); } } public void CheckOnline() { //从Application获取数据表、获取SessionID DataTable dtOnline; dtOnline = (DataTable)System.Web.HttpContext.Current.Application["OnlineTalbe"]; string sessionId = System.Web.HttpContext.Current.Session.SessionID.ToString(); //数据表中是否有我的记录 DataRow drFind = dtOnline.Rows.Find(sessionId); if(drFind != null) { //有;更新我的状态 Info.Write("更新"); drFind["LastActiveTime"] = DateTime.Now; drFind["UserWhere"] = this.AtWhere; //用户由访客状态变为了登陆会员、或反之 if(Extend.GetSession(USERNAME) != null) { drFind["VisitorName"] = Extend.GetSession(USERNAME); drFind["VisitorLevel"] = Extend.GetSession("UserLevel"); } else { drFind["VisitorName"] = "vst"; drFind["VisitorLevel"] = -1; } } else { //无;加入关于我的在线信息 Info.Write("插入"); DataRow drNew = dtOnline.NewRow(); drNew["SessionID"] = sessionId; drNew["VisitorName"] = "vst"; drNew["VisitorLevel"] = -1; drNew["LastActiveTime"] = DateTime.Now; drNew["LoginTime"] = DateTime.Now; drNew["VisitorIP"] = Extend.GetRealIP();; drNew["UserWhere"] = this.AtWhere; dtOnline.Rows.Add(drNew); } //如果没有人正在执行删除且离上次删除的时间间隔超过设定值 TimeSpan tsRemove = DateTime.Now - Convert.ToDateTime(GetApplication("LastRemove")); if(tsRemove.Seconds > REMOVEDIFF && this.GetApplication("Removing").ToString() == "n") { //锁定,我正在删除过期用户 System.Web.HttpContext.Current.Application.Lock(); SetApplication("Removing", "y"); System.Web.HttpContext.Current.Application.UnLock(); //不知道Rows.Count是否随循环减少,如果是效率就大于foreach且这里不能使用foreach for(int i=0; i OFFLINEDIFF) { dtOnline.Rows.Remove(drDel); } } //我删完了,Response.Write("好累") ; System.Web.HttpContext.Current.Application.Lock(); SetApplication("Removing", "n"); SetApplication("LastRemove", DateTime.Now.ToString()); System.Web.HttpContext.Current.Application.UnLock(); } //把被我揉腻完了的数据表放回Application dtOnline.AcceptChanges(); System.Web.HttpContext.Current.Application.Lock(); System.Web.HttpContext.Current.Application["DataTalbeOnline"] = dtOnline; System.Web.HttpContext.Current.Application.UnLock(); } private string GetApplication(string apcname) { return System.Web.HttpContext.Current.Application[apcname].ToString(); } public void SetApplication(string apcname, string apcvalue) { System.Web.HttpContext.Current.Application[apcname] = apcvalue; } /// /// 返回用户当前位置 /// private string AtWhere { get { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(System.Web.HttpContext.Current.Request.ServerVariables["url"]); if(System.Web.HttpContext.Current.Request.ServerVariables["QUERY_STRING"] != String.Empty) { sb.Append("?"); sb.Append(System.Web.HttpContext.Current.Request.ServerVariables["QUERY_STRING"]); } return sb.ToString() ; } } /// /// 创建表 /// public void CashTableInit() { DataTable dt = new DataTable("OnlineTalbe"); dt.Columns.Add("SessionID",typeof(string)); dt.Columns.Add("VisitorName", typeof(string)); dt.Columns.Add("VisitorLevel", typeof(int)); dt.Columns.Add("LastActiveTime", typeof(DateTime)); dt.Columns.Add("LoginTime", typeof(DateTime)); dt.Columns.Add("VisitorIP", typeof(string)); dt.Columns.Add("UserWhere", typeof(string)); dt.Columns["SessionID"].Unique = true; dt.PrimaryKey = new DataColumn[]{dt.Columns["SessionID"]}; System.Web.HttpContext.Current.Application["OnlineTalbe"] = dt; System.Web.HttpContext.Current.Application["LastRemove"] = DateTime.Now.ToString(); System.Web.HttpContext.Current.Application["Removing"] = "n"; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值