我是开发新手,所以请原谅我这是一个愚蠢的问题。 这是我当前项目的片段。我为#34;最喜欢的"而得到一个红色的波浪形在我的switch语句中,当我使用if语句来分配我的"最喜欢的"基于用户输入的变量,但如果我只是直接声明最喜欢的变量,我就不会得到红色的波浪形。错误表示当前上下文中不存在收藏夹。如果以前曾经问过这个问题,我很抱歉,但我对这个论坛也是全新的。提前谢谢您的任何光线消失。
class Program
{
enum Color
{
Red, Orange, Green, Blue, Indigo, Violet
}
static void Main(string[] args)
{
string userChoice = Console.ReadLine();
Console.WriteLine("Please enter the number of your favorite color out of the following choices. /n Where 1 = Red /n 2 = Orange /n 3 = Green /n 4 = Blue /n 5 = Indigo /n 6 = Violet");
if (userChoice == "1")
{
Color favorite = Color.Red;
}
if (userChoice == "2")
{
Color favorite = Color.Orange;
}
if (userChoice == "3")
{
Color favorite = Color.Green;
}
if (userChoice == "4")
{
Color favorite = Color.Blue;
}
if (userChoice == "5")
{
Color favorite = Color.Indigo;
}
if (userChoice == "6")
{
Color favorite = Color.Violet;
}
switch (favorite)
{
case Color.Red:
Console.WriteLine("You chose Red");
break;
case Color.Orange:
Console.WriteLine("You chose Orange");
break;
case Color.Green:
Console.WriteLine("You chose Green");
break;
case Color.Blue:
Console.WriteLine("You chose Blue");
break;
case Color.Indigo:
Console.WriteLine("You chose Indigo");
break;
case Color.Violet:
Console.WriteLine("You chose Violet");
break;
default:
Console.WriteLine("You didn't choose a color");
break;
}
}
}
答案 0 :(得分:0)
为什么我收到当前不存在的“收藏”错误 我的交换机上下文?
因为您已在favorite
语句的正文中声明if
作为局部变量。这意味着变量favorite
无法从这些实体中访问。
我们如何解决这个问题?
我会尝试以下(还有其他选项):
// Here we make use of a nullable.
// since the user hasn't enter any color yet the value of color is null.
Color? color = null;
// Here we ask the user to enter his choice.
string userChoice = Console.ReadLine();
Console.WriteLine("Please enter the number of your favorite color out of the following choices. /n Where 1 = Red /n 2 = Orange /n 3 = Green /n 4 = Blue /n 5 = Indigo /n 6 = Violet");
// According to users input, we set the corresponding color.
// If not a correct color entered, then the value of color would stay null.
if (userChoice == "1")
{
color = Color.Red;
}
if (userChoice == "2")
{
color = Color.Green;
}
if (userChoice == "3")
{
color = Color.Blue;
}
if (userChoice == "4")
{
color = Color.Blue;
}
switch (color)
{
case Color.Red:
Console.WriteLine("You chose Red");
break;
case Color.Green:
Console.WriteLine("You chose Green");
break;
case Color.Blue:
Console.WriteLine("You chose Blue");
break;
case Color.Orange:
Console.WriteLine("You chose Orange");
break;
case null:
Console.WriteLine("You didn't choose a color");
break;
default:
Console.WriteLine("You didn't choose a color");
break;
}
答案 1 :(得分:0)
你定义"最喜欢的"变量"如果"身体,然后你正在使用"切换"在"最喜欢"变量。但是,如果没有人"如果"那就让我们想一想会发生什么?身体火灾? - 正确,#34;最爱"变量将不会定义,但它在"开关"中使用?陈述 - 逻辑错误。这就是编译器阻止你犯这个错误的原因。只需定义"最喜欢的"变量出现"如果"正文然后在"中指定必要的值;如果"体
答案 2 :(得分:0)
“最爱”仅在每个“if”的范围内创建,然后被“销毁”。
if (userChoice == "1") // Here favorite does not exist yet
{
Color favorite = Color.Red; // favorite is created and assigned Color.Red
} // The scope of "if" ends, so all variables created within are destroyed, in this case, "favorite"
所以你真正想做的是
Color? favorite = null;
if (userChoice == "1") // Here favorite already exists
{
favorite = Color.Red; // favorite is assigned Color.Red
} // Same as before, but favorite is not destroyed this time because it was not created(declared) within "if"'s scope
另一方面,您无法按原样使用开关:
switch语句的控制类型由switch表达式建立。
•如果switch表达式的类型是sbyte,byte,short,ushort,int,uint,long,ulong,bool,char,string或enum-type,或者它是与nulllable类型对应的类型这些类型,那就是switch语句的管理类型。
•否则,从switch表达式的类型到以下可能的控制类型之一,必须存在一个用户定义的隐式转换(第6.4节):sbyte,byte,short,ushort,int,uint,long,ulong ,char,string或者,与这些类型之一对应的可空类型。
•否则,如果不存在此类隐式转换,或者存在多个此类隐式转换,则会发生编译时错误。
(从C#5.0规范,8.7.2 switch语句 - 您可以在... \ Microsoft Visual Studio 12.0 \ VC#\ Specifications \ 1033 \ CSharp语言规范.docx中找到它)
所以在这里,假设你只是对if和switch语句进行了一些操作,那么最终结果将更多地是
static void Main(string[] args)
{
Console.WriteLine("Please enter the number of your favorite color out of the following choices. \n Where 1 = Red \n 2 = Orange \n 3 = Green \n 4 = Blue \n 5 = Indigo \n 6 = Violet");
string userChoice = Console.ReadLine();
Color? favorite = null;
if (userChoice == "1")
{
favorite = Color.Red;
}
if (userChoice == "2")
{
favorite = Color.Orange;
}
if (userChoice == "3")
{
favorite = Color.Green;
}
if (userChoice == "4")
{
favorite = Color.Blue;
}
if (userChoice == "5")
{
favorite = Color.Indigo;
}
if (userChoice == "6")
{
favorite = Color.Violet;
}
switch (userChoice)
{
case "1":
Console.WriteLine("You chose Red");
break;
case "2":
Console.WriteLine("You chose Orange");
break;
case "3":
Console.WriteLine("You chose Green");
break;
case "4":
Console.WriteLine("You chose Blue");
break;
case "5":
Console.WriteLine("You chose Indigo");
break;
case "6":
Console.WriteLine("You chose Violet");
break;
default:
Console.WriteLine("You didn't choose a color");
break;
}
Console.ReadKey(true);
}