C# 无法将类型“int”隐式转换为“bool”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12851526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type 'int' to 'bool'
提问by user1730332
Possible Duplicate:
Help converting type - cannot implicitly convert type ‘string' to ‘bool'
I am very new to the language n I am not a good programmer. This code is giving me error:
我对这门语言很陌生,我不是一个好的程序员。这段代码给了我错误:
cannot implicitly convert type int to bool.
不能将 int 类型隐式转换为 bool 类型。
I am not sure what I am doing wrong. Can some tell me what I am doing wrong. Any help would be appreciated n any recomendation would also help.
我不确定我做错了什么。有人可以告诉我我做错了什么。任何帮助将不胜感激,任何推荐也将有所帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class mysteryVal
{
public const int limitOfGuess = 5;
// Data member
public int mystVal;
private int numOfGuess ;
private randomNumberMagnifier mag = new randomNumberMagnifier();
public int randomMag(int num)
{
return num + mystVal;
}
// Instance Constructor
public mysteryVal()
{
mystVal = 0;
numOfGuess = 0;
}
public void game(int user)
{
int userInput = user;
if (numOfGuess < limitOfGuess)
{
numOfGuess++;
if (userInput = mag.randomMagnifier())
{
}
}
}
}
}
采纳答案by Jacob Mattison
The line
线
if (userInput = mag.randomMagnifier())
should be
应该
if (userInput == mag.randomMagnifier())
回答by Ravindra Bagale
Correct this:
更正一下:
if (userInput = mag.randomMagnifier())
to:
到:
if (userInput == mag.randomMagnifier())
Here you are assigning the value in the ifstatement, which is wrong. You have to check the condition, for checking condition u have to use "==".ifstatement returns boolean values, and because you are assigning value here, it's giving the error.
这里你在if语句中赋值,这是错误的。您必须检查条件,为了检查条件,您必须使用"==". if语句返回布尔值,并且因为您在此处分配值,所以它给出了错误。
回答by AntoineLev
you should use == instead of = change:
Lif(userinput = mag.randommagnifier())for
你应该使用 == 而不是 = change:
Lif(userinput = mag.randommagnifier())for
if(userinput == mag.randommagnifier())
回答by Tejas Sharma
An if statement always contains an expression which evaluates to a boolean value. Your line
if 语句总是包含一个计算结果为布尔值的表达式。你的线路
if (userInput = mag.randomMagnifier())
is not a boolwhich is what is causing the error. You probably meant
不是bool导致错误的原因。你可能是说
if (userInput == mag.randomMagnifier())
回答by Edmond Kotowski
The condition
条件
userInput = mag.randomMagnifier()
needs to be
需要是
userInput == mag.randomMagnifier()
What you have is trying to assign the userInput value and then it tries to convert the int to bool. With C# this is not possible.
您所拥有的是尝试分配 userInput 值,然后尝试将 int 转换为 bool。使用 C# 这是不可能的。

