我想看到label6显示用户选择号码的正确次数.而label7显示用户选择错误的次数.它不会增加一个.
错误1运算符'++'不能应用于'string'类型的操作数错误2运算符'++'不能应用于'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));
}
Run Code Online (Sandbox Code Playgroud)
编辑(从OP回答他自己的问题):
我已经尝试了你的建议方式,但每次我猜错了数字都不会增加一个.
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));
}
}
Run Code Online (Sandbox Code Playgroud)
Gab*_*abe 10
它看起来并不像你坚持correct和incorrect
创建属性:
public int Correct { get; set; }
public int Incorrect { get; set;}
Run Code Online (Sandbox Code Playgroud)
然后:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
您正在存储正确和不正确的变量string.
请int改为使用:
int correct = 0;
int incorrect = 0;
Run Code Online (Sandbox Code Playgroud)
并将您的代码更改为:
correct++;
label6.Text = correct.ToString();
Run Code Online (Sandbox Code Playgroud)
和:
incorrect++;
label7.Text = incorrect.ToString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5969 次 |
| 最近记录: |