2012-10-12 53 views
6

Possible Duplicate:
Help converting type - cannot implicitly convert type ‘string’ to ‘bool’不能类型“诠释”隐式转换为“布尔”

我很新的语言ň我不是一个优秀的程序员。此代码给我错误:

cannot implicitly convert type int to bool.

我不知道我在做什么错。 有些人可以告诉我我做错了什么。任何帮助,将不胜感激ñ任何recomendation也将有所帮助。

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()) 
        { 
       } 
       } 

      } 


      } 
       } 
+0

不同的类型,但相同的想法。注意*错误在哪里 - 它会直接引导你解决问题。 – 2012-10-12 03:03:04

+0

您在哪里声明了limitOfGuess的变量? – Nich

+0

我把它声明为一个const。在类声明之后。 – user1730332

回答

9

线

if (userInput = mag.randomMagnifier()) 

应该

if (userInput == mag.randomMagnifier()) 
11

纠正这一点,

if (userInput = mag.randomMagnifier()) 

到:

if (userInput == mag.randomMagnifier()) 

在这里,您正在分配if语句中的值,这是错误的。你必须检查条件,检查条件你必须使用"=="
if语句返回布尔值,并且因为您在此处分配值,所以会给出错误。

3

你应该使用==而不是=变化: Lif(userinput = mag.randommagnifier())

if(userinput == mag.randommagnifier()) 
3

if语句总是包含计算结果为布尔值的表达式。您的线路

if (userInput = mag.randomMagnifier()) 

不是bool这是什么导致错误。你可能意味着

if (userInput == mag.randomMagnifier()) 
3

条件

userInput = mag.randomMagnifier() 

需求是

userInput == mag.randomMagnifier() 

什么你试图分配userInput值,然后它会尝试转换INT为bool。用C#这是不可能的。

相关问题