C#实现猜数字小游戏
作者:fanxingyue 时间:2023-01-13 16:39:38
本文实例为大家分享了C#实现猜数字小游戏的具体代码,供大家参考,具体内容如下
效果如图:
代码:
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 _1csz
{
public partial class Form1 : Form
{
int x;///定义的是一个全局变量
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)///产生一个随机数
{
Random rd = new Random();
x = rd.Next(100);
}
private void button2_Click(object sender, EventArgs e)///显示正确答案
{
label4.Visible = true;
label4.Text = x.ToString();
}
private void button3_Click(object sender, EventArgs e)///退出键
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)///窗体初始化
{
label3.Visible = false;
label4.Visible = false;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)///KeyDown事件:当焦点在文本框时按下任何键都触发该事件
{
if (e.KeyCode==Keys.Enter)///KeyCode属性获取KeyUp和KeyDown事件的键盘代码,其值用Keys枚举成员名
{
if (x==int.Parse(textBox1.Text))
{
label3.Visible = true;
label3.Text = "猜对了,你真棒!";
}
else if (int.Parse(textBox1.Text) > x)
{
label3.Visible = true;
label3.Text = "真是,猜大了!";
}
else
{
label3.Visible = true;
label3.Text = "真是,猜小了!";
}
}
}
}
}
来源:https://blog.csdn.net/a716121/article/details/51476968
标签:C#,猜数字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java黑盒测试之nextDate函数测试
2023-02-10 20:31:08
![](https://img.aspxhome.com/file/2023/9/83459_0s.png)
unity通过Mesh网格绘制图形球体
2023-12-12 08:08:21
![](https://img.aspxhome.com/file/2023/3/82233_0s.jpg)
详解如何继承Mybatis中Mapper.xml文件
2022-12-22 14:48:34
Spring中的使用@Async异步调用方法
2023-07-10 17:20:28
Java Property类使用详解
2023-11-06 21:52:57
![](https://img.aspxhome.com/file/2023/7/58667_0s.png)
java实现清理DNS Cache的方法
2022-07-27 23:11:50
Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法
2023-09-06 19:53:30
![](https://img.aspxhome.com/file/2023/9/65269_0s.png)
探讨:将两个链表非降序合并为一个链表并依然有序的实现方法
2023-06-23 01:41:38
android Retrofit2+okHttp3使用总结
2023-07-31 21:02:10
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
Java中JDBC连接池的基本原理及实现方式
2022-10-17 10:27:03
![](https://img.aspxhome.com/file/2023/8/88758_0s.jpg)
Spring IOC与DI核心深入理解
2023-10-29 14:09:15
Android编程中的消息机制实例详解
2022-11-14 06:50:05
![](https://img.aspxhome.com/file/2023/0/85970_0s.png)
C#发送邮箱实现代码
2022-04-16 11:57:57
C#多线程中的互斥锁Mutex
2022-04-26 06:11:55
![](https://img.aspxhome.com/file/2023/5/69225_0s.png)
Spring AOP实现记录操作日志
2022-06-04 06:03:53
![](https://img.aspxhome.com/file/2023/0/76520_0s.jpg)
C#实现飞行棋源码
2023-11-09 11:17:09
java开发微信公众号支付
2021-10-24 16:02:40
![](https://img.aspxhome.com/file/2023/9/69339_0s.png)
c# 使用Task实现非阻塞式的I/O操作
2023-07-21 23:27:39
Java语言实现简单FTP软件 FTP上传下载队列窗口实现(7)
2021-07-25 04:01:58
![](https://img.aspxhome.com/file/2023/3/63343_0s.png)