簡體   English   中英

C#錯誤1在此作用域中已經定義了一個名為'userin'的局部變量

[英]C# Error 1 A local variable named 'userin' is already defined in this scope

錯誤1在此作用域中已經定義了一個名為'userin'的局部變量

    myclass.fillAccounts();

        //int i = 0;
        //while (i != 1)

        string userin = null;

        while (userin !="x") 
        {
            //use the following menu:            
            Console.WriteLine("*****************************************");
            Console.WriteLine("enter an a or A to search account numbers");
            Console.WriteLine("enter a b or B to average the accounts");
            Console.WriteLine("enter an x or X to exit program");
            Console.WriteLine("*****************************************");
            Console.Write("Enter option-->");
            userin = Console.ReadLine();
            if (userin == "a" || userin == "A")
            {
                myclass.searchAccounts();
            }
            else if (userin == "b" || userin == "B")
            {
                myclass.averageAccounts();
            }
            else if (userin == "x" || userin == "X")
            {
              break;
            }

            else
            {
                Console.WriteLine("You entered an invalid option");
            }
        }
    }
}

}

在您未提供的代碼中該方法的其他地方,您已經有一行代碼,內容如下:

string userin;

您稍后說:

string userin = null;

試圖重新定義局部變量。 擺脫第一個變量聲明,您應該可以解決這個問題。 (您希望將聲明保留為初始值,因為您需要while評估while條件時初始化該變量。)

如果這是方法中的所有代碼,則可能在類上有一個名為userin的字段。 這是部分課程嗎? 它是WPF / Winforms / Webforms類嗎? 也許您有一個名為userin的控件?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM