John Tran
John Tran

Reputation: 31

Saving previous number and adding

So I'm making this "gambling" program in school our mission is to have 3 dices roll and if the middle dice is bigger than the first one but smaller than the 3rd one you win.

    private void BtnKasta_Click(object sender, EventArgs e)
    {
        Random slump = new Random();
        int T1 = slump.Next(1, 7);
        int T2 = slump.Next(1, 7);
        int T3 = slump.Next(1, 7);
        lblDice1.Text = T1.ToString();
        lblDice2.Text = T2.ToString();
        lblDice3.Text = T3.ToString();
        double pengar = double.Parse(tbxPengar.Text);

        if (T2>T1 && T3>T2)
        {
            double satsning = double.Parse(tbxSatsa.Text);
            double vinst = satsning * 5;
            double total = pengar - satsning + vinst;
            lblPengar.Text = total.ToString();

The problem I have is I don't know how to make it so that it saves the total from the last "win". For example if I put in 2000 and bet 100 I will get 2400 because 2000-100 + (100*5) but if i press the button again and win again I still have 2400, so how do I make it so that I would get 2800

Upvotes: 3

Views: 51

Answers (1)

Jacky
Jacky

Reputation: 3259

I believe this is a win-form application, not a web form.

Running debug will show you what went wrong, but let do it via your example.

Let's go through via your example:

put in 2000 and bet 100 and you win 2 times

/*First Time*/
double pengar = double.Parse(tbxPengar.Text);
//tbxPengar.Text = "2000"
//pengar = 2000
double satsning = double.Parse(tbxSatsa.Text);
//tbxSatsa.Text = "100"
//satsning = 100
double vinst = satsning * 5;
//vinst = 100 * 5 = 500
double total = pengar - satsning + vinst;
//total = 2000 - 100 + 500 = 2400
lblPengar.Text = total.ToString();
//lblPengar.Text = "2400"

Let see the 2nd time:

/*Second Time*/
double pengar = double.Parse(tbxPengar.Text);
//tbxPengar.Text = "2000"
//pengar = 2000
...

You could see, on 2nd time, you doesn't update back lblPengar.Text to your textbox tbxPengar.Text, therefore, the code will just run with existing value (e.g "2000").

So just add tbxPengar.Text = lblPengar.Text to the last line will work.

Happy coding

Upvotes: 1

Related Questions