if语句错误:int转换为boolean

时间:2015-08-16 21:07:25

标签: c# int boolean

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隐式转换为boolean。我根本不想这样做。我很失落。救命啊!

2 个答案:

答案 0 :(得分:2)

您使用单个theCorrectAnswerfinalUserInput变量的值设置为=。并且此语句返回值 - 整数1。

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

这是一个拼写错误或语法错误。

答案 1 :(得分:0)

对于您的情况,您需要等于运算符==但是您正在使用赋值运算符= in if语句:

if (finalUserInput = theCorrectAnswer)

应该如下:

if (finalUserInput == theCorrectAnswer)