本帖最後由 css6666 於 2012-7-5 11:44 PM 編輯
這是我自己寫的,大家可以參考看看,如有問題,可以回覆。
程式:
- 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 WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- int guess,min=1,max=99;
- private void Form1_Load(object sender, EventArgs e)
- {
- Random r = new Random();
- guess=r.Next(1, 100);
- textBox2.Text = "請輸入1到99的數字!!"+ Environment.NewLine;
- }
- private void button2_Click(object sender, EventArgs e) //送出按鈕
- {
- if ((int.Parse(textBox1.Text) > 99) || (int.Parse(textBox1.Text) < 1))
- {
- textBox2.Text += "你輸入錯誤" + Environment.NewLine;
- }
- else if ((int.Parse(textBox1.Text) > max) || (int.Parse(textBox1.Text) <min))
- {
- textBox2.Text += "你輸入錯誤" + Environment.NewLine;
- }
- else
- {
- if (guess > int.Parse(textBox1.Text))
- {
- min = int.Parse(textBox1.Text);
- textBox2.Text += "請輸入" + min + "到" + max + "的數字" + Environment.NewLine;
- }
- else if (guess < int.Parse(textBox1.Text))
- {
- max = int.Parse(textBox1.Text);
- textBox2.Text += "請輸入" + min + "到" + max + "的數字" + Environment.NewLine;
- }
- else if (guess == int.Parse(textBox1.Text))
- {
- textBox2.Text += "你爆了,答案是"+guess + Environment.NewLine;
- }
- }
- textBox1.Focus();
- textBox1.SelectAll();
- }
- private void button1_Click(object sender, EventArgs e) //解答按鈕
- {
- textBox1.Text = guess.ToString();
- }
- private void button4_Click(object sender, EventArgs e) //重玩按鈕
- {
- Random r = new Random();
- guess = r.Next(1, 100);
- textBox2.Text = "請輸入1到99的數字!!" + Environment.NewLine;
- }
- private void button3_Click(object sender, EventArgs e) //離開按鈕
- {
- Application.Exit();
- }
- }
- }
複製代碼
... |