C#窗體-猜數字


1.用到的控件:groupbox、label、textbox、button、menustrip等

2.實現的功能,隨機產生一個數字,輸入自己猜的答案,判斷是否猜對。

3.運行結果

4.代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace network_programming_1._1_guess_number
12 {
13     public partial class Form1 : Form
14     {
15         int number = 0;
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void 使用說明ToolStripMenuItem_Click(object sender, EventArgs e)
22         {
23             MessageBox.Show("1、點擊按鈕“產生隨機數”隨機產生一個1-100的數" + "\r\n" + "2、在答題區輸入答案,並提交");
24         }
25 
26         //題目區
27         private void button1_Click(object sender, EventArgs e)
28         {
29             Random rd = new Random();
30             number=rd.Next(1, 100);//(生成1~100之間的隨機數,不包括100)
31             textBox1.Text = string.Format("已經產生了一個1-100(不包括100)的整數,請在答題區輸入你的答案!");
32             button1.Enabled = false;//設置按鈕“產生隨機數”變灰
33         }
34 
35         //答題區
36         private void button2_Click(object sender, EventArgs e)
37         {
38             string a = textBox2.Text;
39             int number1;
40             bool x = int.TryParse(a, out number1);//輸入整數就會返回true,number1會保持輸入的值,否則number會變成0
41             if(x)
42             {
43                 number1 = int.Parse(a);
44                 //判斷輸入的結果
45                 if (number1 >= 1 && number1 < 100)
46                 {
47                     if (number1 > number)
48                     {
49                         MessageBox.Show("高了");
50                         textBox2.Clear();
51                     }
52                     else if (number1 < number)
53                     {
54                         MessageBox.Show("低了");
55                         textBox2.Clear();
56                     }
57                     else
58                     {
59                         MessageBox.Show("恭喜您答對了!!!");
60                         textBox2.Clear();
61                         button2.Enabled = false;
62                     }
63                 }
64                 else
65                 {
66                     MessageBox.Show("2請輸入1-100(不包括100)的整數");
67                     textBox2.Clear();
68                 }
69             }
70             else
71             {
72                 MessageBox.Show("1請輸入1-100(不包括100)的整數");
73                 textBox2.Clear();
74             }
75             
76         }
77 
78         //退出程序
79         private void button3_Click(object sender, EventArgs e)
80         {
81             this.Close();
82         }
83 
84         private void button4_Click(object sender, EventArgs e)
85         {
86             button1.Enabled = true;
87             button2.Enabled = true;
88             textBox1.Clear();
89             textBox2.Clear();
90         }
91     }
92 }

5.總結

實現了簡單的C#窗體程序設計,編寫過程中注意到了一些地方:

(1)輸入猜測的數字之后要判斷時候輸入有效

(2)每次產生隨機數之后,要保證完成一次答題之后,才能再產生數字,要求按鈕變灰不可用

(3)也可以在還沒有答完一次題就結束本次答題,但是要手動操作,在這里加了按鈕:“重新開始”

(4)每次輸入答案提交后,文本要清空。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM