0

我在 C# 中设置了一个变量,该变量根据选择的 If 语句而变化。

但是当我尝试 Console.WriteLine 变量时,它告诉我该变量在当前上下文中不存在,有人可以帮我解决这个问题吗?

public void mood()
{
  var unhappiness = Hunger + Boredom;

  if (unhappiness < 5)
  {
    string m = "Happy";
  }

  if (unhappiness <= 5 && unhappiness <= 10)
  {
    string m = "Okay";
  }

  if (unhappiness <= 11 && unhappiness <= 15)
  {
    string m = "Frustrated";
  }

  if (unhappiness <= 16)
  {
    string m = "Mad";
  }

  Console.WriteLine(m);
}

变量“m”是我遇到的问题。

4

9 回答 9

3

you need to declare string m outside - after your method decleration:

public string mood()
        {
            var unhappiness = Hunger + Boredom;
            string m = string.Empty;
            if (unhappiness < 5)
            {
                m = "Happy";
            }

            if (unhappiness <= 5 && 
                unhappiness <= 10)
            {
                m = "Okay";
            }

            if (unhappiness <= 11 &&
                unhappiness <= 15)
            {
                m = "Frustrated";
            }

            if (unhappiness <= 16)
            {
                m = "Mad";
            }
            return m;
于 2013-11-12T16:02:22.267 回答
3

This:

{

}

Is putting your variable inside that scope, so it won't be available outside of it. That's why you can declare 4 variables named m like you just did. Each m is a different guy, inside its little bracket world.

I think you may want to declare it beforehand:

string m="";

if (unhappiness < 5)
{
    m = "Happy";
}

if (unhappiness <= 5 && 
    unhappiness <= 10)
{
    m = "Okay";
}

if (unhappiness <= 11 &&
   unhappiness <= 15)
{
    m = "Frustrated";
}

if (unhappiness <= 16)
{
    m = "Mad";
}
于 2013-11-12T16:02:24.487 回答
1

Define string M before the if-statements. If you initialize it just after var unhappiness, and set it within the if-statements. You define string m within the if-statements, but when they end, string m is gone as well.

于 2013-11-12T16:02:28.893 回答
1

Try this instead :

public void mood()
{
    var unhappiness = Hunger + Boredom;

    string m = string.Empty;

    if (unhappiness < 5)
    {
        m = "Happy";
    }

    if (unhappiness >= 6 && unhappiness <= 10)
    {
        m = "Okay";
    }

    if (unhappiness >= 11 && unhappiness <= 15)
    {
        m = "Frustrated";
    }

    if (unhappiness >= 16)
    {
        m = "Mad";
    }

    Console.WriteLine(m);
}

The problem was that m was defined inside an if statement, it scope was limited to that statement.

于 2013-11-12T16:02:48.237 回答
0

As it is written, the variable m only exists within the scope of each if statement.

You should declare m within the global scope of the method:

 public void mood()
 {
      string m = "default mood";
      // ...
 }

This will make it available to all child scopes (delimited by the curly braces), including each of the if statements.

In the code that you wrote initially, each time you wrote string m in a different scope, you were declaring a new variable called m. Each time you reached the end of the scope, that variable was destroyed. So when you got to the WriteLine statement, m effectively did not exist.

于 2013-11-12T16:02:27.543 回答
0
      public void mood()
    {
        var unhappiness = Hunger + Boredom;
        string m;
        if (unhappiness < 5)
        {
            m = "Happy";
        }

        if (unhappiness <= 5 && 
            unhappiness <= 10)
        {
            m = "Okay";
        }

        if (unhappiness <= 11 &&
            unhappiness <= 15)
        {
            m = "Frustrated";
        }

        if (unhappiness <= 16)
        {
            m = "Mad";
        }

        Console.WriteLine(m);
于 2013-11-12T16:02:28.537 回答
0

字符串 m您的 if 子句中定义。另外,你不能从你的方法中返回一个字符串,所以我改变了:)

尝试

public string mood()
{
    var unhappiness = Hunger + Boredom;
    string m = string.Empty;

    if (unhappiness < 5)
    {
        m = "Happy";
    }

        // etc

    return m;
}
于 2013-11-12T16:03:38.647 回答
0

m不存在。这就是为什么你不能把它打印出来或用它做任何事情。它不存在。没有这样的变量m

在某些内部作用域中,碰巧有几个变量被调用m。但只要定义它们的块退出,它们就消失了。现在他们属于一个无法返回的过去。他们的记忆像垃圾收集海洋中的水滴一样飘散了。

于 2013-11-12T16:07:49.243 回答
0

要解决您的问题,您可以执行以下操作:

public void mood()
{
  var unhappiness = Hunger + Boredom;
  string m = "Unknown";

  if (unhappiness < 5)
  {
    m = "Happy";
  }    
  else if (unhappiness >= 5 && unhappiness <= 10) // >= 5 not <= 5
  {
    m = "Okay";
  }
  else if (unhappiness > 11 && unhappiness <= 15) // > 10 or >= 11 not <= 11
  {
    m = "Frustrated";
  }
  else if (unhappiness >= 16) // assume it should be enything else
  {
    m = "Mad";
  }

  Console.WriteLine(m);
}
  • 变量的定义和变量的使用必须发生在用大括号 { } 定义的同一范围内。
    因此,在这种情况下,您可以在整个方法情绪中使用 m,但不能在方法情绪之外使用。
  • if, else if 如果你想先检查一些东西,并且只有当结果为假时你想检查其他东西作为后备,if 会派上用场。
于 2013-11-12T17:34:18.610 回答