想請教一下c# form 要怎麼加入一個紀錄第幾次猜中的機制呢?
int ans;
int max = 100;
int min = 0;
int keyin = 0;
public Form1()
{
InitializeComponent();
}
private void btn_ok_Click(object sender, EventArgs e)
{
keyin = int.Parse(textBox1.Text);
textBox1.Text = "";
if (keyin != ans)//判斷是否答對
{
if (keyin > min && keyin < max)//判斷是否在上下限之中
{
if (keyin > ans)
max = keyin;
if (keyin < ans)
min = keyin;
lab_hint.Text = "請猜" + min + "至" + max + "之間。";
lbl_history.Text = lbl_history.Text + keyin + ",";
}
else
{
lab_hint.Text = "請猜" + min + "至" + max + "之間。\n超出上下限!";
}
}
else
{
lab_hint.Text = "恭喜你,猜中了!!";
}
}
private void Form1_Load(object sender, EventArgs e)
{
ans = innerRand.Next(1, 99);
lab_hint.Text = "請猜" + min + "至" + max + "之間。";
lbl_history.Text = "";
lab_ans.Text = ans.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ans = innerRand.Next(1, 99);
lab_ans.Text = ans.ToString();
max = 100;
min = 0;
lbl_history.Text = "";
lab_hint.Text = "請猜" + min + "至" + max + "之間。";
}
}
}
猜N次後猜中的意思?
int ans;
int max = 100;
int min = 0;
int keyin = 0;
count = 0;
public Form1()
{
InitializeComponent();
}
private void btn_ok_Click(object sender, EventArgs e)
{
count++;
keyin = int.Parse(textBox1.Text);
textBox1.Text = "";
if (keyin != ans)//判斷是否答對
{
if (keyin > min && keyin < max)//判斷是否在上下限之中
{
if (keyin > ans)
max = keyin;
if (keyin < ans)
min = keyin;
lab_hint.Text = "請猜" + min + "至" + max + "之間。";
lbl_history.Text = lbl_history.Text + keyin + ",";
}
else
{
lab_hint.Text = "請猜" + min + "至" + max + "之間。\n超出上下限!";
}
}
else
{
lab_hint.Text = "恭喜你,猜中了!! 一共猜了" + count + "次";
}
}
private void button1_Click(object sender, EventArgs e)
{
ans = innerRand.Next(1, 99);
lab_ans.Text = ans.ToString();
max = 100;
min = 0;
count = 0;
lbl_history.Text = "";
lab_hint.Text = "請猜" + min + "至" + max + "之間。";
}