运算符’==’不能应用于’string’类型的操作数和’int’

private void button1_Click(object sender, EventArgs e) { Random random = new Random(); int getal1 = random.Next(0, 100); Random random2 = new Random(); int getal2 = random2.Next(0, 100); int Antwoord = getal1 + getal2; if (Antwoord == textBox1.Text); ... } 

它说

运算符’==’不能应用于’string’和’int’类型的操作数

有人能帮我吗?

 if (Antwoord.ToString() == textBox1.Text); 

写这样的。 你想检查int到字符串,这不可能发生。 您应该将int值转换为string或将字符串值转换为int。 我建议你将int转换为字符串,在其他情况下你可以有一个例外。

如果需要在TextBox输入的整数值,则应尝试解析其Text

 int textBox1Value; if (int.TryParse(textBox1.Text, out textBox1Value)) { // Here the text was successfully parsed to the textBox1Value variable if (Antwoord == textBox1Value) { ... // do your stuff } }