1、c猜数游戏代码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 猜数字public partial class Form1 : Formpublic Form1()InitializeComponent();private static bool _begin
2、flag;private static int _guesstimes = 1;public int correctnumber = new int4;public void BeginButton_Click(object sender, EventArgs e)_guesstimes = 0;#region 生成随机数Random rand = new Random();correctnumber0 = rand.Next(0, 9);correctnumber1 = rand.Next(0, 9);correctnumber2 = rand.Next(0, 9);correctnumbe
3、r3 = rand.Next(0, 9);while (correctnumber0 = correctnumber1 |correctnumber0 = correctnumber2 |correctnumber0 = correctnumber3)correctnumber0 = rand.Next(0, 9);while (correctnumber1 = correctnumber0 |correctnumber1 = correctnumber2 |correctnumber1 = correctnumber3)correctnumber1 = rand.Next(0, 9);whi
4、le (correctnumber2 = correctnumber0 |correctnumber2 = correctnumber1 |correctnumber2 = correctnumber3)correctnumber2 = rand.Next(0, 9);while (correctnumber3 = correctnumber1 |correctnumber3 = correctnumber2 |correctnumber3 = correctnumber0)correctnumber3 = rand.Next(0, 9);#endregiontextBox5.Text +=
5、"开始游戏." + "rn"textBox5.Text += "提示信息." + "rn"for (int i = 0; i < correctnumber.Length; i+)textBox6.Text += correctnumberi.ToString();_beginflag = true;BeginButton.Enabled = false;private void GuessButton_Click(object sender, EventArgs e)int allcorrectcount
6、= 0;int numbercorrectcount = 0;if (_beginflag = false)MessageBox.Show("游戏尚未开始!");else if(textBox1.Text = textBox2.Text |textBox1.Text = textBox3.Text |textBox1.Text = textBox4.Text | textBox2.Text = textBox3.Text |textBox2.Text = textBox4.Text |textBox3.Text = textBox4.Text ) MessageBox.Sh
7、ow("请输入不重复的四个数字且不能为空!");textBox1.Text = ""textBox2.Text = ""textBox3.Text = ""textBox4.Text = ""else if (textBox1.Text = "" |textBox2.Text = "" |textBox3.Text = "" |textBox4.Text = "")MessageBox.Show("各个数字
8、均不能为空!");textBox1.Text = ""textBox2.Text = ""textBox3.Text = ""textBox4.Text = ""elseif (textBox1.Text = correctnumber0.ToString() &&textBox2.Text = correctnumber1.ToString() &&textBox3.Text = correctnumber2.ToString() &&textBox4.T
9、ext = correctnumber3.ToString()MessageBox.Show("恭喜你猜对了!");_guesstimes = 0;for (int i = 0; i < 4; i+)if (textBox1.Text = correctnumberi.ToString()if (i = 0)allcorrectcount += 1;elsenumbercorrectcount += 1;for (int i = 0; i < 4; i+)if (textBox2.Text = correctnumberi.ToString()if (i = 1
10、)allcorrectcount += 1;elsenumbercorrectcount += 1;for (int i = 0; i < 4; i+)if (textBox3.Text = correctnumberi.ToString()if (i = 2)allcorrectcount += 1;elsenumbercorrectcount += 1;for (int i = 0; i < 4; i+)if (textBox4.Text = correctnumberi.ToString()if (i = 3)allcorrectcount += 1;elsenumberco
11、rrectcount += 1; textBox5.Text += "本次有" + allcorrectcount + "个数字和位置都正确,有" + numbercorrectcount + "个数字正确,位置不正确。你还有" + (9 - _guesstimes) + "次机会!" + "rn"_guesstimes+;if (allcorrectcount = 4)textBox5.Text += "恭喜你,你使用了" + _guesstimes + "次就猜
12、出来了!"private void ClearButton_Click(object sender, EventArgs e)textBox1.Text = ""textBox2.Text = ""textBox3.Text = ""textBox4.Text = ""private void TryAgainButton_Click(object sender, EventArgs e)textBox5.Text = ""_guesstimes = 0;textBox1.Text =
13、 ""textBox2.Text = ""textBox3.Text = ""textBox4.Text = ""#region 生成随机数Random rand = new Random();correctnumber0 = rand.Next(0, 9);correctnumber1 = rand.Next(0, 9);correctnumber2 = rand.Next(0, 9);correctnumber3 = rand.Next(0, 9);while (correctnumber0 = correct
14、number1 |correctnumber0 = correctnumber2 |correctnumber0 = correctnumber3)correctnumber0 = rand.Next(0, 9);while (correctnumber1 = correctnumber0 |correctnumber1 = correctnumber2 |correctnumber1 = correctnumber3)correctnumber1 = rand.Next(0, 9);while (correctnumber2 = correctnumber0 |correctnumber2
15、= correctnumber1 |correctnumber2 = correctnumber3)correctnumber2 = rand.Next(0, 9);while (correctnumber3 = correctnumber1 |correctnumber3 = correctnumber2 |correctnumber3 = correctnumber0)correctnumber3 = rand.Next(0, 9);#endregiontextBox5.Text += "rn" + "开始游戏." + "rn"t
16、extBox5.Text += "提示信息." + "rn"for (int i = 0; i < correctnumber.Length; i+)textBox6.Text += correctnumberi.ToString();_beginflag = true;BeginButton.Enabled = false;namespace 猜数字partial class Form1/ <summary>/ 必需的设计器变量。 / </summary>private System.ComponentModel.ICon
17、tainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#regio
18、n Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()this.label1 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.textBox1 = new System.Windows.Forms.TextBox();this.textBox2 = new System.Wind
19、ows.Forms.TextBox();this.textBox3 = new System.Windows.Forms.TextBox();this.textBox4 = new System.Windows.Forms.TextBox();this.textBox5 = new System.Windows.Forms.TextBox();this.GuessButton = new System.Windows.Forms.Button();this.ClearButton = new System.Windows.Forms.Button();this.TryAgainButton =
20、 new System.Windows.Forms.Button();this.BeginButton = new System.Windows.Forms.Button();this.textBox6 = new System.Windows.Forms.TextBox();this.SuspendLayout();/ / label1/ this.label1.AutoSize = true;this.label1.Font = new System.Drawing.Font("黑体", 21.75F, System.Drawing.FontStyle.Regular,
21、 System.Drawing.GraphicsUnit.Point, (byte)(134);this.label1.ForeColor = System.Drawing.Color.Green;this.label1.Location = new System.Drawing.Point(12, 9);this.label1.Name = "label1"this.label1.Size = new System.Drawing.Size(216, 29);this.label1.TabIndex = 0;this.label1.Text = "数字猜猜猜游戏
22、"/ / label2/ this.label2.AutoSize = true;this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)(134);this.label2.ForeColor = System.Drawing.Color.FromArgb(int)(byte)(0), (int)(byte)(192), (int)(byte)(0);this.la
23、bel2.Location = new System.Drawing.Point(12, 79);this.label2.Name = "label2"this.label2.Size = new System.Drawing.Size(216, 48);this.label2.TabIndex = 1;this.label2.Text = "请在下面的输入框中输入一个rn包含数字0-9的不要重复的4位rn数:"/ / textBox1 / this.textBox1.Location = new System.Drawing.Point(50, 141
24、);this.textBox1.Name = "textBox1"this.textBox1.Size = new System.Drawing.Size(21, 21);this.textBox1.TabIndex = 2;/ / textBox2/ this.textBox2.Location = new System.Drawing.Point(77, 141);this.textBox2.Name = "textBox2"this.textBox2.Size = new System.Drawing.Size(21, 21);this.textB
25、ox2.TabIndex = 2;/ / textBox3/ this.textBox3.Location = new System.Drawing.Point(104, 141);this.textBox3.Name = "textBox3"this.textBox3.Size = new System.Drawing.Size(21, 21);this.textBox3.TabIndex = 2;/ / textBox4/ this.textBox4.Location = new System.Drawing.Point(131, 141);this.textBox4.
26、Name = "textBox4"this.textBox4.Size = new System.Drawing.Size(21, 21);this.textBox4.TabIndex = 2;/ / textBox5/ this.textBox5.BackColor = System.Drawing.Color.FromArgb(int)(byte)(255), (int)(byte)(224), (int)(byte)(192);this.textBox5.Font = new System.Drawing.Font("楷体", 10.5F, Sys
27、tem.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)(134);this.textBox5.ForeColor = System.Drawing.Color.Red;this.textBox5.Location = new System.Drawing.Point(234, 73);this.textBox5.Multiline = true;this.textBox5.Name = "textBox5"this.textBox5.Size = new System.Drawing.
28、Size(497, 289);this.textBox5.TabIndex = 3;/ / GuessButton/ this.GuessButton.Location = new System.Drawing.Point(32, 221);this.GuessButton.Name = "GuessButton"this.GuessButton.Size = new System.Drawing.Size(154, 43);this.GuessButton.TabIndex = 4;this.GuessButton.Text = "猜一猜"this.G
29、uessButton.UseVisualStyleBackColor = true;this.GuessButton.Click += new System.EventHandler(this.GuessButton_Click);/ / ClearButton/ this.ClearButton.Location = new System.Drawing.Point(32, 270);this.ClearButton.Name = "ClearButton"this.ClearButton.Size = new System.Drawing.Size(154, 43);this.ClearButton.TabIndex = 4;this.ClearButton.Text = "清 空"
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2