Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# if语句错误:整数转换为布尔值_C#_Int_Boolean - Fatal编程技术网

C# if语句错误:整数转换为布尔值

C# if语句错误:整数转换为布尔值,c#,int,boolean,C#,Int,Boolean,嗨。当我尝试用条件设置if语句时,会弹出一条错误消息,说明无法将int隐式转换为boolean。我根本不想那样做。我完全迷路了。救命啊 您正在使用single=将正确答案变量的值设置为finalUserInput。该语句返回值–整数1 您需要使用==来比较两个值 这是一个输入错误或语法错误。您需要相等运算符==来处理您的案例,但您在if语句中使用赋值运算符=: public static void Main(string[] args) // this is a method called "M

嗨。当我尝试用条件设置if语句时,会弹出一条错误消息,说明无法将int隐式转换为boolean。我根本不想那样做。我完全迷路了。救命啊

您正在使用single
=
将正确答案变量的值设置为
finalUserInput
。该语句返回值–整数1

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


这是一个输入错误或语法错误。

您需要相等运算符==来处理您的案例,但您在if语句中使用赋值运算符=:

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 (finalUserInput = theCorrectAnswer)

您正在做的是
finalUserInput=正确答案
,即作业。因此,您的if最终会变成“if(int)”,这就是为什么会出现该错误。您应该将
=
替换为
=
if (finalUserInput == theCorrectAnswer)