Increase the number by 1

I would like label6 to display the number of correct times when the user selects a number. And label7 displays the number of cases when the user chooses the wrong one. It does not increase by one.

Error 1 The operator '++' cannot be applied to an operand of type 'string' Error 2 The operator '++' cannot be applied to an operand of type 'string'

private void button1_Click(object sender, EventArgs e)
    {
        string correct="0";
        string incorrect="0";
        RandomNumber(0,99);
        button2.Enabled = true ;
        button1.Enabled = false;
        label3.Visible = true;
        if (textBox1.Text == label1.Text)
            label3.Text=("Winner");
               label6.Text = correct +1;
               if (textBox1.Text != label1.Text)
                   label7.Text = incorrect= +1;
            label3.Text=(string.Format("Sorry - You Lose, The number is {0}",   label1.Text));

    }


Edit (from OP answer to your question):

I tried as you suggest, but the number does not increase by one every time I think it is wrong.

private void button1_Click(object sender, EventArgs e)


    {
        int correct=0;
        int incorrect=0;
        RandomNumber(0,99);
        button2.Enabled = true ;
        button1.Enabled = false;
        label3.Visible = true;
        if (textBox1.Text == label1.Text)
        {
            label3.Text = ("Winner");
            label6.Text = (++correct).ToString(); 
        }

        else if (textBox1.Text != label1.Text)
        {
            label7.Text = (incorrect+1).ToString(); 

            label3.Text = (string.Format("Sorry - You Lose, The number is {0}", label1.Text));
        }


    }
+3
source share
5 answers

, correct incorrect . , , . . , , . ( )

// instance variables
private int correct = 0;
private int incorrect = 0;

private void button1_Click(object sender, EventArgs e)
{
    RandomNumber(0,99);
    button2.Enabled = true ;
    button1.Enabled = false;
    label3.Visible = true;
    if (textBox1.Text == label1.Text)
    {
        label3.Text=("Winner");
        label6.Text = (++correct).ToString(); // convert int to string
    }
    // indentation does not indicate the block
    else //if (textBox1.Text != label1.Text)
    {
        label3.Text=(string.Format("Sorry - You Lose, The number is {0}",   label1.Text));
        label7.Text = (++incorrect).ToString();
    }
}
+4

, correct incorrect

:

public int Correct { get; set; }
public int Incorrect { get; set;}

:

private void button1_Click(object sender, EventArgs e)
{
   RandomNumber(0,99);
   button2.Enabled = true ;
   button1.Enabled = false;
   label3.Visible = true;

   if (textBox1.Text == label1.Text)
   {
     label3.Text=("Winner");
     label6.Text = (++this.Correct).ToString();
   }
   else
   {  
      label3.Text=(string.Format("Sorry - You Lose, The number is {0}", label1.Text));
      label7.Text = (++this.Incorrect).ToString();
   } 
}
+10

string.

int :

int correct = 0;
int incorrect = 0;

:

correct++;
label6.Text = correct.ToString();

incorrect++;
label7.Text = incorrect.ToString();
+8

:

correct = correct + 1; (I think this is what you tried to achieve) false = false + 1;

OR

correct + = 1; insect + = 1;

+1
source
label6.Text = correct +1;

only sets label6 for correction + 1; he does not change the meaning of the correct. If you use:

int correct;
label6.Text = (++correct).ToString();

you should see what you want.

0
source

Source: https://habr.com/ru/post/1773302/


All Articles