本文实例为大家分享了C#实现猜数字小游戏的具体代码,供大家参考,具体内容如下
效果如图:
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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