iT邦幫忙

0

請問c# form猜數字遊戲要怎麼紀錄第幾次猜中的

c#
  • 分享至 

  • xImage

想請教一下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 + "之間。";
    }
}

}

Homura iT邦高手 1 級 ‧ 2021-08-05 16:00:07 檢舉
加個計數用的的變數
每次按鈕猜的時候+1不就知道了
int guesstime = 0 ;//放最上面

每button click一次就guesstime++ ;
mika41130 iT邦新手 5 級 ‧ 2021-08-05 17:06:11 檢舉
好哦 謝謝你!!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Hankz
iT邦新手 2 級 ‧ 2021-08-05 16:21:36

猜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 + "之間。";
}
mika41130 iT邦新手 5 級 ‧ 2021-08-05 17:05:39 檢舉

對的!謝謝你

我要發表回答

立即登入回答