If语句错误:int转换为布尔值

本文关键字:转换 布尔值 int 语句 错误 If | 更新日期: 2023-09-27 18:08:07

public static void Main(string[] args) // this is a method called "Main". It is called when the program starts.
{
    Random numberGenerator = new Random();
    int userInput1;
    int userInput2;
    int finalUserInput;
    int theCorrectAnswer;
    //generating random numbers. from 1 to 10. 11 is exclusive.
    userInput1 = numberGenerator.Next(1, 11);
    userInput2 = numberGenerator.Next(1, 11);
    //Asks the user to solve the multiplication problem.
    Console.Write("What is " + userInput1 + " x " + userInput2 + " ?");
        finalUserInput = Convert.ToInt32(Console.ReadLine());
        theCorrectAnswer = userInput1 * userInput2;
    if(finalUserInput = theCorrectAnswer)

嗨。当我尝试设置一个带有条件的if语句时,弹出一条错误消息,说您不能隐式地将int转换为布尔值。我根本没想那么做。我完全迷路了。的帮助!

If语句错误:int转换为布尔值

通过单个=将变量theCorrectAnswer的值设置为finalUserInput。此语句返回值-整型1。

您需要使用==来比较两个值。

需要使用相等运算符==,但在if语句中使用赋值运算符=

if (finalUserInput = theCorrectAnswer)

应该像下面这样:

if (finalUserInput == theCorrectAnswer)