C# 局部变量名';选择';无法声明,因为它将赋予';选择';
对不起,我对编程界完全陌生。我正在创建一个控制台应用程序,它基本上接受您的输入,决定您是想将摄氏度转换为华氏度,还是想将摄氏度转换为华氏度 我遇到的问题是: “错误3无法在此字段中声明名为'choice'的局部变量 范围,因为它将赋予“选择”不同的含义,即 已在“父级或当前”范围中用于表示其他内容” 我试着看其他的例子,但它们比我的大脑现在能理解的复杂得多C# 局部变量名';选择';无法声明,因为它将赋予';选择';,c#,if-statement,while-loop,do-while,C#,If Statement,While Loop,Do While,对不起,我对编程界完全陌生。我正在创建一个控制台应用程序,它基本上接受您的输入,决定您是想将摄氏度转换为华氏度,还是想将摄氏度转换为华氏度 我遇到的问题是: “错误3无法在此字段中声明名为'choice'的局部变量 范围,因为它将赋予“选择”不同的含义,即 已在“父级或当前”范围中用于表示其他内容” 我试着看其他的例子,但它们比我的大脑现在能理解的复杂得多 namespace TemperatureApp { class Program { static void
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();
}
}
}
应该读
choice = Convert.ToInt32(Console.ReadLine());
第二次放置
int
,就是在{}内声明另一个名为“choice”的变量。该定义与外部定义冲突。请尝试将choice变量声明从循环范围中删除。像这样:
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();
}
}局部变量不能与另一个局部变量同名。可以使用与较大非局部范围(例如类成员)的变量同名的局部变量,但在这种情况下,局部变量会隐藏较大范围的变量 您要做的是从第二个
选项中去掉int
。您不需要重新声明它,只需要分配它
choice = Convert.ToInt32(Console.ReadLine());
都在错误消息中,您在同一范围内声明了两次choice变量
当您从控制台输入读取值时,您不需要重新声明变量,只需要设置它。详细说明:
int choice; // declare variable
do
{
int choice = ...; // re-declare variable
}
在第二行,您希望为变量赋值,而不是重新声明它,为此,只需替换此行:
int choice = ...
与
我遇到的问题是:
“无法在此范围内声明名为'choice'的局部变量,因为它将赋予'choice'不同的含义,而'choice'已在'parent or current'范围内用于表示其他内容。”
我试着看其他的例子
您不应该看示例,而应该看错误和代码。C#编译器的消息非常用户友好,这一条也非常清楚。你只需要理解所使用的语言
无法声明名为“choice”的局部变量
您可以这样声明一个变量:variable-type-variable-name
,它发生在do
-循环中的错误行上:
int choice = Convert.ToInt32(Console.ReadLine());
这里用int
声明变量,int
是它的类型。删除int
将使该行成为一行,这正是您所需要的。Convert.ToInt32(Console.ReadLine())
的结果被分配(=)给选项
-变量。您只能在声明变量后分配它,但在代码的前面已经这样做了。所以错误是:
因为它将赋予“choice”不同的含义,它已经在“parent或current”范围中用于表示其他内容
如果您在代码中查找,您会看到在(static void Main()
)中声明了一个同名变量:
int choice;
现在您应该知道只需要声明一次变量,而且通常最好在尽可能小的范围内完成。在您的情况下,只需在
int choice=Convert.ToInt32(Console.ReadLine())之前删除int
,因为您还需要访问do
-循环之外的choice
。这里失败了吗:如果(choice==2)
?我不同意。在他的循环条件下使用;在循环体中所做的声明在那里将不可见。@BenVoigt oops,没有看到。谢谢。非常感谢您的深入分析。这绝对有助于学习。当然。在此处选择您最喜欢的答案,然后单击复选标记。它告诉其他人这个问题已经得到了回答。True@user2054277,如果你投票给其他你也觉得有帮助的人也很好。
int choice = Convert.ToInt32(Console.ReadLine());
int choice;