下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)C#實現(xiàn)三匹馬賽跑,使用進(jìn)度條和Timer實現(xiàn)編寫比賽代碼:3匹馬比賽,隨機產(chǎn)生勝者,使用進(jìn)度條和Timer實現(xiàn):在Visual Studio 2005 中新建WinForm應(yīng)用程序:界面如下:用label1,label2,label3來顯示三匹馬的速度 窗體還有3個Timer控件,分別作為三匹馬的時鐘:源代碼如下:using System;using System.Collections.Generic;using System.ComponentModel;usin
2、g System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WinApp public partial class Form賽馬 : Form public Form賽馬() InitializeComponent(); int horse = new int3; /馬的速度 decimal secondCount1 = 0; decimal secondCount2 = 0; decimal secondCount3 = 0; / / 窗體Load事件 設(shè)置時間間隔為0.1
3、秒 為進(jìn)度條賦值最大值 / / / private void Form賽馬_Load(object sender, EventArgs e) progressBar1.Maximum = 100; progressBar2.Maximum = 100; progressBar3.Maximum = 100; timer1.Interval = 100;/間隔 0.1秒 timer2.Interval = 100; timer3.Interval = 100; / / 開始比賽事件:文本提示框恢復(fù)默認(rèn) 進(jìn)度條歸零, / 重新隨機出三匹馬的速度 先停止時鐘 再啟動計時器 / / / private
4、 void btnBegin_Click(object sender, EventArgs e) richTextBox1.Text = ; richTextBox2.Text = ; richTextBox3.Text = ; progressBar1.Value = 0; progressBar2.Value = 0; progressBar3.Value = 0; secondCount1 = 0; secondCount2 = 0; secondCount3 = 0; /清空重新賦值 bool b = new bool21; Random ran = new Random(); for
5、 (int i = 0; i horse.Length; i+) int number = ran.Next(1, 21);/假設(shè)馬的速度為120之間 各不相同 bool bx = bnumber; while (bx) /如果bnumber 為true 說明 number被取到 請重新取數(shù) number = ran.Next(1, 21); bx = bnumber; bnumber = true; horsei = number; label1.Text = string.Format(馬1的速度:0米每秒, horse0); label2.Text = string.Format(馬2的
6、速度:0米每秒, horse1); label3.Text = string.Format(馬3的速度:0米每秒, horse2); timer1.Stop();/停止計時器 timer2.Stop(); timer3.Stop(); timer1.Start();/啟動計時器 比賽開始 timer2.Start(); timer3.Start(); / / 時鐘事件 當(dāng)馬已跑完時,計時器停止運行 / / / private void timer1_Tick(object sender, EventArgs e) secondCount1 += 0.1M; decimal x = horse0
7、 * secondCount1; if (x = progressBar1.Maximum) progressBar1.Value = progressBar1.Maximum; richTextBox1.Text = string.Format(馬1已跑完全程0米,速度:1米每秒,用時2秒, 100, horse0, secondCount1); timer1.Enabled = false; else progressBar1.Value = (int)x; Application.DoEvents(); private void timer2_Tick(object sender, Ev
8、entArgs e) secondCount2 += 0.1M; decimal y = horse1 * secondCount2; if (y = progressBar2.Maximum) progressBar2.Value = progressBar2.Maximum; richTextBox2.Text = string.Format(馬2已跑完全程0米,速度:1米每秒,用時2秒, 100, horse1, secondCount2); timer2.Enabled = false; else progressBar2.Value = (int)y; Application.DoEvents(); private void timer3_Tick(object sender, EventArgs e) secondCount3 += 0.1M; decimal z = horse2 * secondCount3; if (z = progressBar3.Maximum) progressBar3.Value = progressBar3.Maximum; richTextBox3.Text = string.Format(馬3已跑完全程0米,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教師年度考核個人述職報告范文(19篇)
- 班主任教師工作簡述7篇
- 智慧安全政務(wù)云數(shù)據(jù)中心整體解決方案
- 某文旅景區(qū)概念性策略-策劃案例
- 《治安管理處罰法》知識競賽參考試題庫150題(含答案)
- 2025年武漢鐵路職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 2025年無錫城市職業(yè)技術(shù)學(xué)院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 2025年曹妃甸職業(yè)技術(shù)學(xué)院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 特種材料供應(yīng)合同
- 二手設(shè)備的買賣合同范本
- (正式版)HG∕T 20644-2024 彈簧支吊架選用標(biāo)準(zhǔn)
- 項目采購管理培訓(xùn)
- (高清版)DZT 0399-2022 礦山資源儲量管理規(guī)范
- 面試官面試技巧(精簡版)課件
- 中央空調(diào)節(jié)能改造方案
- 高校圖書館服務(wù)
- 環(huán)衛(wèi)一體化運營方案
- 科技進(jìn)步類現(xiàn)代軌道交通綜合體設(shè)計理論與關(guān)鍵技術(shù)公
- 源代碼審計報告模板
- 干式變壓器知識大全課件
- 重大危險源公示牌(完整)-2
評論
0/150
提交評論