




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上精选优质文档-倾情为你奉上专心-专注-专业专心-专注-专业精选优质文档-倾情为你奉上专心-专注-专业C#实现三匹马赛跑,使用进度条和Timer实现编写比赛代码:3匹马比赛,随机产生胜者,使用进度条和Timer实现:在Visual Studio 2005 中新建WinForm应用程序:界面如下:用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事件 设置时间间隔为0.1
3、秒 为进度条赋值最大值 / / / 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; / / 开始比赛事件:文本提示框恢复默认 进度条归零, / 重新随机出三匹马的速度 先停止时钟 再启动计时器 / / / 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);/假设马的速度为120之间 各不相同 bool bx = bnumber; while (bx) /如果bnumber 为true 说明 number被取到 请重新取数 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(); / / 时钟事件 当马已跑完时,计时器停止运行 / / / 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO 5821:2025 EN Resistance welding - Spot welding electrodes - Female electrode caps
- 【正版授权】 ISO/IEC TR 23090-27:2025 EN Information technology - Coded representation of immersive media - Part 27: Media and architectures for render-based systems and applications
- 【正版授权】 ISO/TR 4752:2025 EN Biotechnology - Inventory of methods for detection of microbiological contamination in mammalian cell culture
- 【正版授权】 ISO 11680:2025 EN Machinery for forestry - Safety requirements and testing for portable pole mounted powered pruners
- 【正版授权】 ISO 13400-2:2025 EN Road vehicles - Diagnostic communication over Internet Protocol (DoIP) - Part 2: Transport protocol and network layer services
- 古筝课件教学计划
- 2020-2025年期货从业资格之期货法律法规全真模拟考试试卷B卷含答案
- 【深圳】2025年4月广东深圳光明区政务服务和数据管理局招聘一般类岗位专干5人笔试历年典型考题及考点剖析附带答案详解
- 电位滴定法仪器分析测试技术系列32课件
- Brand KPIs for milk:a2 in the United States-英文培训课件2025
- 2025年广东省深圳市中考历史试卷(含解析)
- 百万销售日常管理办法
- 天津市南开区2024-2025学年七年级下学期期末考试数学试卷及答案
- 安全培训-重大事故隐患判定标准-专家版
- 2025年数据科学与大数据技术试题及答案
- 土木工程结构力学课件
- 【课件】《科学记数法》说课课件2024-2025学年人教版数学七年级上册
- 消防检测和消防评估服务方案
- 旧钢板桩买卖合同范本
- 安卓课程设计开发指南
- 健康服务合作协议书
评论
0/150
提交评论