C#实现猜数字小游戏
本文实例为大家分享了C#实现猜数字小游戏的具体代码,供大家参考,具体内容如下
效果如图:
代码:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespace_1csz { publicpartialclassForm1:Form { intx;///定义的是一个全局变量 publicForm1() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse)///产生一个随机数 { Randomrd=newRandom(); x=rd.Next(100); } privatevoidbutton2_Click(objectsender,EventArgse)///显示正确答案 { label4.Visible=true; label4.Text=x.ToString(); } privatevoidbutton3_Click(objectsender,EventArgse)///退出键 { Application.Exit(); } privatevoidForm1_Load(objectsender,EventArgse)///窗体初始化 { label3.Visible=false; label4.Visible=false; } privatevoidtextBox1_KeyDown(objectsender,KeyEventArgse)///KeyDown事件:当焦点在文本框时按下任何键都触发该事件 { if(e.KeyCode==Keys.Enter)///KeyCode属性获取KeyUp和KeyDown事件的键盘代码,其值用Keys枚举成员名 { if(x==int.Parse(textBox1.Text)) { label3.Visible=true; label3.Text="猜对了,你真棒!"; } elseif(int.Parse(textBox1.Text)>x) { label3.Visible=true; label3.Text="真是,猜大了!"; } else { label3.Visible=true; label3.Text="真是,猜小了!"; } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。