【问题标题】:The name 'input' does not exist in the current context当前上下文中不存在名称“输入”
【发布时间】:2020-03-29 22:59:33
【问题描述】:

我已经使用 C# 编程大约一年了,我通常会因为忽略某处的细节而遇到此类问题。我觉得我又在这样做了,但我似乎无法解决问题。我有一个名为“input”的字符串变量,我在“Main”类的“if”语句中声明了它,如下所示:
string input = Console.Readline();
作为一个单独的 'if' 语句,在同一个 'Main' 类中,我写了这个:

    if (input != null || input != 0)
    {
        //I have code for this part, but it is irrelevant 
    }
    else
    {
        //And this part, but it is also irrelevant
    }

我的问题是 MonoDevelop 在第二个“if”语句中突出显示了两个“输入”变量,并说“当前上下文中不存在名称输入”。我觉得我忽略了一些东西,任何帮助将不胜感激。 我的完整代码是这样的:

using System;

namespace ConsoleTest
{
class MainClass
{
    public static string version = "0.0.1";
    public static string precursor = "/:>";

    public static void Main (string[] args)
    {
        Console.Write ("Console Test ");
        Console.WriteLine (version);
        Console.Write (precursor);
        string start = Console.ReadLine ();

        if (start == "start") {
            while (true) {
                Console.WriteLine ("Started");
                Console.Write (precursor);
                string input = Console.ReadLine ();
            }
        } else {
            Environment.Exit (0);
        }

        if (input != null || input != 0) {
            //Code
        } else {
            Console.WriteLine("Error: Input null");
        }

    }
}
}

【问题讨论】:

  • 我们需要在现场看到它们。您没有提到是否有不同的方法或其他结构代码可能会改变变量的范围。
  • 我们没有线索你在哪里声明input与这段代码在哪里。基本上,在如此少的背景下,我们无法为您提供帮助。例如,您是否在构造函数中将input 声明为局部变量,然后尝试在单独的方法中访问它?或者它可能是一个实例变量,而您正试图在静态方法中使用它?如果它一个字符串变量,你为什么要把它和0比较?
  • 请显示完整的上下文
  • 请用您的代码文件的全部内容编辑您的问题
  • 好吧。 OP 确实说他们在另一个 if 语句中声明了 input。所以,这就是问题所在。由于input 仅在 if 语句的 inside 范围内,因此在它之外无法使用。你需要在外面声明,然后在里面设置。

标签: c#


【解决方案1】:

在您的第一个 if 语句之前声明您的输入变量。因为它是在里面声明的,所以它只能在你的 if 语句中使用(或者在这种情况下在你的循环中)

【讨论】:

    【解决方案2】:

    input 仅存在于您的第一个 while loop 范围内的 if statement 中。

    把它移到外面。

    string input = new string();
    
    if (start == "start") {
        while (true) 
        {
                Console.WriteLine ("Started");
                Console.Write (precursor);
                input = Console.ReadLine ();
        }
    }
    

    【讨论】:

      【解决方案3】:

      需要更多代码,但这可能是因为您试图将 String 与 int 进行比较。也许你正在寻找

      input.equals("0")?
      

      编辑:查看您的代码,您在 if 语句中初始化了变量。您必须在函数开始时对其进行初始化。即使您只是将其设置为 null。

      public static void Main (string[] args)
      {
          Console.Write ("Console Test ");
          Console.WriteLine (version);
          Console.Write (precursor);
          string start = Console.ReadLine ();
          string input = null;
      
          if (start == "start") {
              while (true) {
                  Console.WriteLine ("Started");
                  Console.Write (precursor);
                  input = Console.ReadLine ();
              }
          } else {
              Environment.Exit (0);
          }
      
          if (input != null || !input.equals("0")) {
              //Code
          } else {
              Console.WriteLine("Error: Input null");
          }
      
      }
      

      【讨论】:

      • 将字符串与 int 进行比较时不会出现该错误。您会收到一条错误消息,指出您无法将字符串与 int 进行比较。 ;)
      • 我也会小心地说“在你的函数开始时”之类的话。他只需要在使用变量之前在 if 语句之外声明它。 =)
      • 哈哈,是的,你说的很对,以后我会努力讲得更清楚。
      • 只是想在这里提供帮助,因为您正在回答一个不熟悉该语言编码的人。 =)
      【解决方案4】:

      您的字符串输入在循环中声明。因此,在这个循环之后它是未知的。 在你的循环之外声明它(并将它启动到nullString.Empty)然后将它填充到你的循环中

      【讨论】:

      • 或者在第一个 if 语句中,甚至。除了作为第一个范围内的第二个范围之外,循环几乎没有什么后果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2014-04-22
      • 2017-06-17
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多