C#模擬實(shí)現(xiàn)抽獎(jiǎng)小程序的示例代碼
1.抽獎(jiǎng)主界面
2.操作步驟
S鍵開始;
0、1、2、3、4、5鍵分別對(duì)應(yīng)6次獎(jiǎng)項(xiàng);
分別是 特等獎(jiǎng)、一等獎(jiǎng)、二等獎(jiǎng)、三等獎(jiǎng)、四等獎(jiǎng)、五等獎(jiǎng)
9鍵是加抽獎(jiǎng);
空格退出抽獎(jiǎng)結(jié)果界面;
P鍵關(guān)閉氣泡效果。
2.1 抽獎(jiǎng)界面
2.2 抽獎(jiǎng)結(jié)果導(dǎo)出
*************特等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:2021/12/30 22:41:22)*************** 工號(hào):100899 姓名:石臻臻的雜貨鋪 領(lǐng)域:后端 *************一等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:2021/12/30 22:42:09)*************** 工號(hào):100931 姓名:牧羊人_阿標(biāo) 領(lǐng)域:移動(dòng) *************三等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:2021/12/30 22:42:17)*************** 工號(hào):100978 姓名:前端老實(shí)人 領(lǐng)域:前端 工號(hào):100952 姓名:Oliver尹 領(lǐng)域:前端 工號(hào):100990 姓名:愿許浪盡天涯 領(lǐng)域:運(yùn)維與安全 工號(hào):101024 姓名:?jiǎn)虇碳业凝埮? 領(lǐng)域:其他 *************特等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:2021/12/30 22:42:46)*************** 工號(hào):100900 姓名:川川菜鳥 領(lǐng)域:大數(shù)據(jù) *************六等獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:2021/12/30 22:42:51)*************** 工號(hào):101013 姓名:憂傷額蝸牛 領(lǐng)域:移動(dòng) 工號(hào):101017 姓名:ML.star 領(lǐng)域:后端 工號(hào):100921 姓名:堅(jiān)果前端の博客 領(lǐng)域:移動(dòng) 工號(hào):100986 姓名:Mr數(shù)據(jù)楊 領(lǐng)域:全棧 工號(hào):100969 姓名:大數(shù)據(jù)小禪 領(lǐng)域:大數(shù)據(jù) 工號(hào):100898 姓名:小小明-代碼實(shí)體 領(lǐng)域:其他 工號(hào):100949 姓名:執(zhí)久呀 領(lǐng)域:后端 工號(hào):100977 姓名:yang_z_1 領(lǐng)域:大數(shù)據(jù) 工號(hào):100944 姓名:可可卷 領(lǐng)域:AI 工號(hào):100988 姓名:曲鳥 領(lǐng)域:全棧 工號(hào):101018 姓名:余光、 領(lǐng)域:前端 工號(hào):100962 姓名:拈花傾城 領(lǐng)域:運(yùn)維與安全 工號(hào):100918 姓名:艾醒 領(lǐng)域:AI 工號(hào):100979 姓名:林深時(shí)不見鹿 領(lǐng)域:后端 工號(hào):100964 姓名:繁星藍(lán)雨 領(lǐng)域:大數(shù)據(jù) 工號(hào):101022 姓名:互聯(lián)網(wǎng)-小阿宇 領(lǐng)域:運(yùn)維與安全 工號(hào):100980 姓名:振華OPPO 領(lǐng)域:移動(dòng) 工號(hào):100923 姓名:_陳哈哈 領(lǐng)域:全棧 工號(hào):100930 姓名:Java執(zhí)梗 領(lǐng)域:后端 工號(hào):100917 姓名:1_bit 領(lǐng)域:全棧
3.源碼
3.1 數(shù)據(jù)庫(kù)連接
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OleDb; using System.Collections; namespace BoeLottery.Model { public class LotteryDataContext { public LotteryDataContext() { if (!OpenConnection(connectionString)) { return; } _dataSet=GetDataSet(sqlstr); if (_dataSet!=null) { _dataTable = _dataSet.Tables[0]; rowData = new List<KeyValuePair<int, int>>(); } MaxNO=IMaxNO = GetMaxNoDB(); GetDataByte(); MaxNO = GetMaxNoDB()+1; } private OleDbDataAdapter dataAdapter = null; private OleDbConnection connection = null; private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data//lottery.mdb;Jet OLEDB:Database Password=123456"; string sqlstr = "select * from lottery "; DataSet _dataSet = null; DataTable _dataTable = null; public int MaxNO {get;set;} public int IMaxNO { get; set; } /// <summary> /// Open Connection /// </summary> /// <param name="connectionString"></param> /// <returns></returns> public bool OpenConnection(string connectionString) { try { connection = new OleDbConnection(connectionString); connection.Open(); } catch (System.Exception ex) { if (connection!=null) { connection.Close(); } return false; throw ex; } return true; } public bool CloseConnection() { if (connection!=null) { connection.Close(); } return true; } public DataSet GetDataSet(string sqlString) { try { DataSet dataSet = new DataSet(); dataAdapter = new OleDbDataAdapter(sqlString, connection); dataAdapter.Fill(dataSet); return dataSet; } catch (System.Exception ex) { throw ex; } } public List<KeyValuePair<int,int>> rowData { get; set; } public OleDbDataReader GetDataReader(string sqlString) { OleDbCommand command = new OleDbCommand(sqlString, connection); OleDbDataReader dataReader = command.ExecuteReader(); return dataReader; } public void GetDataByte() { string data = Helper.Logger.ReadLog("System.Data.DataMangentExtensions.dll"); string[] dataSplit=data.Split(';'); foreach (string item in dataSplit) { GetAdDB(Convert.ToInt32(item)); } } public int GetMaxNoDB() { //if (_dataSet.Tables["lottery"].Rows.Count == 0) return 0; 對(duì)不對(duì) return _dataTable.Rows.Count; } /// <summary> /// 根據(jù)ID獲取用戶信息 /// </summary> /// <param name="employeeId"></param> /// <returns></returns> public Employee GetQueryData(int employeeId) { Employee employee = new Employee(); employee.EmployeeID = employeeId; IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where (int)p.Field<Int16>("userid") == employeeId select p; foreach(var data in queryData) { employee.EmployeeNo=data.Field<string>("userno"); employee.EmployeeName = data.Field<string>("username"); employee.EmployeeDep = data.Field<string>("userdep"); } return employee; } /// <summary> /// 根據(jù)EmployNo獲取用戶信息 /// </summary> /// <param name="employNo"></param> /// <returns></returns> public Employee GetQueryDataByNo(int employNo) { IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p; Employee employee = new Employee(); foreach (var data in queryData) { employee.EmployeeNo= data.Field<string>("userno"); employee.EmployeeID = (int)data.Field<Int16>("userid"); employee.EmployeeName = data.Field<string>("username"); employee.EmployeeDep = data.Field<string>("userdep"); } return employee; } public void GetAdDB(int employNo) { IEnumerable<DataRow> queryData = from p in _dataTable.AsEnumerable() where p.Field<string>("userno") == employNo.ToString() select p; if (queryData.Count() == 0) return; DataRow row = _dataTable.NewRow(); foreach(var data in queryData) { row["userno"]=data.Field<string>("userno"); row["userid"] = GetMaxNoDB()+1; rowData.Add(new KeyValuePair<int,int>((int)data.Field<Int16>("userid"), GetMaxNoDB() + 1)); //rowData.Add(new KeyValuePair<int,int>(GetMaxNoDB() + 1,(int)data.Field<Int16>("userid"))); //row["userid"] = (int)data.Field<Int16>("userid"); row["username"] = data.Field<string>("username"); row["userdep"] = data.Field<string>("userdep"); } _dataTable.Rows.Add(row); } } }
3.2 抽獎(jiǎng)程序
//各獎(jiǎng)項(xiàng)所有的人數(shù) { 特等獎(jiǎng)、一等獎(jiǎng)、二等獎(jiǎng)、三等獎(jiǎng)、四等獎(jiǎng) }
public int[] WinnerNums = {4, 4, 12, 16, 40, 60, 80};
//抽獎(jiǎng)次數(shù)
public int[] LotteryTimes = {4, 4, 3, 4, 2, 3, 4};
public int[] HasNotWonNums=new int[7] ;//= { 5, 1, 1, 1, 12 };
#endregion
#region 設(shè)置綁定ICommand
public ICommand StartLotteryCommand { get; private set; } // 開始抽獎(jiǎng)
public ICommand SpecialLotteryCommand { get; private set; } //特等獎(jiǎng)
public ICommand FirstLotteryCommand { get; private set; }
public ICommand SecondLotteryCommand { get; private set; }
public ICommand ThirdLotteryCommand { get; private set; }
public ICommand FouthLotteryCommand { get; private set; }
public ICommand SouvenirLotteryCommand { get; private set; }
public ICommand SixvenirLotteryCommand { get; private set; }
public ICommand ExtendLotteryCommand { get; private set; } //加抽獎(jiǎng)
#endregion
public MainViewModel()
{
//HasNotWonNums = LotteryTimes; //未中獎(jiǎng)的賦值
for (int i = 0; i < 7;i++ )
{
HasNotWonNums[i] = LotteryTimes[i];
}
//GetWinner();
//添加八個(gè)時(shí)鐘
AddTimer(10);
StartLotteryCommand = new RelayCommand(
()=>{
TimerStart();
}
);
SpecialLotteryCommand = new RelayCommand(
()=>
{
logger.WriteLog("*************特等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("特等", "showResultGrade");
Lotterying(0);
//添加顯示邏輯
}
);
ExtendLotteryCommand = new RelayCommand(
() =>
{
logger.WriteLog("*************加抽獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
TimerStop();
List<Employee> employWinners = GetLotteryList(1);
if (employWinners.Count == 0)
{
return;
}
Messenger.Default.Send("加抽", "showResultGrade");
ShowResult(employWinners);
//添加顯示邏輯
}
);
FirstLotteryCommand = new RelayCommand(
()=>
{
logger.WriteLog("*************一等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("一等", "showResultGrade");
Lotterying(1);
}
);
SecondLotteryCommand = new RelayCommand(
()=>
{
logger.WriteLog("*************二等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("二等", "showResultGrade");
Lotterying(2);
}
);
ThirdLotteryCommand = new RelayCommand(
()=>
{
logger.WriteLog("*************三等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("三等", "showResultGrade");
Lotterying(3);
}
);
FouthLotteryCommand = new RelayCommand(
() =>
{
logger.WriteLog("*************四等獎(jiǎng)獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("四等", "showResultGrade");
Lotterying(4);
}
);
SouvenirLotteryCommand = new RelayCommand(
()=>
{
logger.WriteLog("*************五等獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("五等", "showResultGrade");
Lotterying(5);
}
);
//public ICommand SixvenirLotteryCommand { get; private set; }
SixvenirLotteryCommand = new RelayCommand(
() =>
{
logger.WriteLog("*************六等獎(jiǎng)獲得者:(抽獎(jiǎng)時(shí)間:" + DateTime.Now.ToString() + ")***************");
Messenger.Default.Send("六等", "showResultGrade");
Lotterying(6);
}
);
Messenger.Default.Register<string>(this, "AppClose",
(msg) =>
{
logger.Close();
lotteryDataContext.CloseConnection();
}
);
}
以上就是C#模擬實(shí)現(xiàn)抽獎(jiǎng)小程序的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#抽獎(jiǎng)小程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
unity使用射線實(shí)現(xiàn)貼花系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了unity使用射線實(shí)現(xiàn)貼花系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)
這篇文章主要介紹了如何使用C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)的方法,并附上全部源碼,分享給大家,有需要的小伙伴可以參考下。2015-12-12Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C# .NET及Mono跨平臺(tái)實(shí)現(xiàn)原理解析
這篇文章主要介紹了C# .NET及Mono、跨平臺(tái)實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05C#中的delegate委托類型基本學(xué)習(xí)教程
這篇文章主要介紹了C#中的delegate委托類型基本學(xué)習(xí)教程,委托是C#語(yǔ)言所具有的一個(gè)重要特性,需要的朋友可以參考下2016-01-01Python設(shè)計(jì)模式編程中的備忘錄模式與對(duì)象池模式示例
這篇文章主要介紹了Python設(shè)計(jì)模式編程中的備忘錄模式與對(duì)象池模式,文中分別舉了表單和線程的相關(guān)示例,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)XML與實(shí)體類之間相互轉(zhuǎn)換的方法(序列化與反序列化)
這篇文章主要介紹了C#實(shí)現(xiàn)XML與實(shí)體類之間相互轉(zhuǎn)換的方法,涉及C#序列化與反序列化操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02C#使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼
這篇文章主要為大家詳細(xì)介紹了C#如何使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03Windows系統(tǒng)中使用C#讀取文本文件內(nèi)容的小示例
這篇文章主要介紹了Windows系統(tǒng)中使用C#讀取文本文件內(nèi)容的小示例,包括一次一行地讀取文本文件的方法,需要的朋友可以參考下2016-02-02