简体   繁体   English

无法声明局部变量名称'choice',因为它会给'choice'赋予不同的含义

[英]Local variable name 'choice' cannot be declared because it would give different meaning to 'choice'

Sorry, I'm completely new to the programming world. 对不起,我是编程世界的新手。 I'm creating a console application that basically takes your input on whether you want to convert Celsius to Fahrenheit or vice-versa. 我正在创建一个控制台应用程序,基本上可以输入您是否要将摄氏温度转换为华氏温度,反之亦然。

The problem I'm running into is: 我遇到的问题是:

"Error 3 A local variable named 'choice' cannot be declared in this scope because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else" “错误3无法在此范围内声明名为'choice'的局部变量,因为它会给'choice'赋予不同的含义,'choice'已经在'parent或current'范围内用于表示其他内容”

I tried looking at other examples, but they were far more complicated than my brain can understand for now. 我试着看其他例子,但它们远比我大脑现在所能理解的要复杂得多。

namespace TemperatureApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;

            do
            {
                Console.WriteLine("Hi! This is a temperatue app");
                Console.WriteLine("Press 1 for C to F or 2 for F to C");
                //take the user input
                int choice = Convert.ToInt32(Console.ReadLine());


                if (choice == 1)
                {
                    Console.WriteLine("Great, you chose C to F. Now enter the temp.");
                    int celcius = Convert.ToInt32(Console.ReadLine());
                    //next line use the formula and show answer
                }
                if (choice == 2)
                {
                    Console.WriteLine("Great, you chose F to C. Now enter the temp.");
                    int fahrenheit = Convert.ToInt32(Console.ReadLine());
                    //next line use the formula and show answer
                }
                else
                    Console.WriteLine("That is not the right choice.");
                    //In this way, keep asking the person for either 1 or two

            }
            while (choice != 1 || choice != 2);

            Console.ReadLine();
        }
    }
}
int choice = Convert.ToInt32(Console.ReadLine());

should read 应该读

choice = Convert.ToInt32(Console.ReadLine());

By putting the int in the second time, you are declaring another variable called "choice" inside the {}. 通过第二次输入int ,您将在{}内声明另一个名为“choice”的变量。 That definition is conflicting with the one just outside. 这个定义与外面的定义相冲突。

It's all in the error message, you have declared your choice variable twice within the same scope. 这一切都在错误消息中,您已在同一范围内声明了您的选择变量两次。

When you read the value from the console input you don't need to re-declare the variable you just need set it. 当您从控制台输入读取值时,您不需要重新声明您只需要设置它的变量。 To elaborate: 详细说明:

int choice; // declare variable

do
{
    int choice = ...; // re-declare variable
}

On the second line you want to assign the variable a value, not re-declare it, to do that you just need to replace this line: 你想将变量赋值的第二行,没有重新申报的,要做到这一点,你只需要更换这行:

int choice = ...

With

choice = ...

The problem I'm running into is: 我遇到的问题是:

"A local variable named 'choice' cannot be declared in this scope because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else". “在这个范围内不能声明一个名为'choice'的局部变量,因为它会给'choice'赋予不同的含义,'choice'已经在'parent或current'范围中用来表示其他东西”。

I tried looking at other examples 我试着看其他例子

You shouldn't look at examples, but at the error and your code. 您不应该查看示例,而是查看错误和代码。 The C# compiler is quite user-friendly with its messages, and this one is quite clear also. C#编译器的消息对用户非常友好,而且这一点也非常清楚。 You just have to understand the language used. 您只需要了解所使用的语言。

A local variable named 'choice' cannot be declared 无法声明名为“choice”的局部变量

You declare a variable like this: variable-type variable-name , which happens on the line of the error, within your do -loop: 你声明一个这样的变量: variable-type variable-name ,它发生在你的do -loop中的错误行上:

int choice = Convert.ToInt32(Console.ReadLine());

Here with int you declare the variable, int being its type. 这里使用int 声明变量, int是它的类型。 Removing int will make that line an assignment , which is what you need. 删除int将使该行成为一个赋值 ,这正是您所需要的。 The result of Convert.ToInt32(Console.ReadLine()) is assigned (=) to the choice -variable. Convert.ToInt32(Console.ReadLine())的结果赋给(=) choice -variable。 You can only assign a variable after declaring it, but you have already done that earlier in the code. 您只能在声明变量后指定变量,但您已在代码中更早地完成了变量。 So the error says: 所以错误说:

because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else 因为它会给“选择”赋予不同的含义,“选择”已在“父或当前”范围内用于表示其他内容

And if you look up in your code, you see that in a parent scope (in static void Main() ) a variable with the same name is declared: 如果你查看代码,你会看到在父作用域 (在static void Main() )声明了一个具有相同名称的变量:

int choice;

Now you should know you only need to declare a variable once, and that this is usually, preferably done in a scope as small as possible. 现在您应该知道您只需要声明一次变量,并且通常最好在尽可能小的范围内完成。 In your case, you can simply remove the int before int choice = Convert.ToInt32(Console.ReadLine()); 在您的情况下,您可以在int choice = Convert.ToInt32(Console.ReadLine());之前删除int int choice = Convert.ToInt32(Console.ReadLine()); , because you also need to access choice outside the do -loop. ,因为你还需要访问do -loop之外的choice

Try taking the choice variable declaration out of your cycle scope. 尝试从您的循环范围中选择变量声明。 Like this: 像这样:

   int choice;

   do
    {
        Console.WriteLine("Hi! This is a temperatue app");
        Console.WriteLine("Press 1 for C to F or 2 for F to C");
        //take the user input
        choice = Convert.ToInt32(Console.ReadLine());


        if (choice == 1)
        {
            Console.WriteLine("Great, you chose C to F. Now enter the temp.");
            int celcius = Convert.ToInt32(Console.ReadLine());
            //next line use the formula and show answer
        }
        if (choice == 2)
        {
            Console.WriteLine("Great, you chose F to C. Now enter the temp.");
            int fahrenheit = Convert.ToInt32(Console.ReadLine());
            //next line use the formula and show answer
        }
        else
            Console.WriteLine("That is not the right choice.");
            //In this way, keep asking the person for either 1 or two

    }
    while (choice != 1 || choice != 2);

    Console.ReadLine();
}

} }

You can't have a local variable named the same name as another local variable. 您不能拥有与另一个本地变量同名的局部变量。 You can have a local variable with the same name as a variable of a larger, non-local scope (class member for instance) but in that case the local variable hides the larger-scoped variable. 可以使用与较大的非本地范围(例如,类成员)的变量同名的局部变量,但在这种情况下,局部变量会隐藏较大范围的变量。

What you want to do is take the int off the second choice . 你想要做的是从第二个choice取出int You don't need to re-declare it, you just want to assign it. 你不需要重新声明它,你只想分配它。

choice = Convert.ToInt32(Console.ReadLine());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在此范围内声明名为“ i”的局部变量,因为它将赋予“ i”不同的含义 - Local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i' C#变量作用域:无法在此范围内声明“ x”,因为它将赋予“ x”不同的含义 - C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x' 不能在此 scope 中声明名为“turnContext”的本地或参数,因为该名称 - A local or parameter named 'turnContext' cannot be declared in this scope because that name 为什么这个错误“局部变量不能在这个范围内声明,因为它已经被使用了”? - Why is this error “local variable cannot be declared in this scope because it … is already used”? 不能在此 scope 中声明名为“nextjunction”的本地或参数,因为该名称用于封闭本地 scope 以定义本地 - A local or parameter named 'nextjunction' cannot be declared in this scope because that name is used in an enclosing local scope to define a local 无法在此范围内声明名为“ g”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数 - A local or parameter named 'g' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter 不能在此 scope 中声明名为“student”的本地或参数,因为该名称用于封闭的本地 scope 以定义本地参数 - A local or parameter named 'student' cannot be declared in this scope because that name is used in an enclosing local scope to define a local param 如何在本地数据库中创建“多选”列? - How would I create “multiple choice” columns in Local Database? 变量名称不能在此范围内声明 - Variable name cannot be declared in this scope 声明之前无法使用局部变量 - Cannot use the local variable before it is declared
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM