gpt4 book ai didi

c# - 在 switch-case 条件中编写 switch-case 条件的可能性?

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:31 25 4
gpt4 key购买 nike

我有这段代码,它按预期的方式运行。但是,如果用户在插入第一个值后单击 1、2 或 3,我想执行一些额外的操作。如您所见,如果用户第二次按 2,将显示按名字排列的玩家列表(至少这是我的意图)。所以我的问题是,这是否可以通过在前一个中插入一个 switch-case 条件来完成?我尝试过这样的事情:请参阅下面的更多代码。

 var isValidMenuItem = false;

while (!isValidMenuItem)
isValidMenuItem = Menu();

Console.WriteLine();
Console.ReadKey(true);

}

private static bool Menu()
{
Console.WriteLine("You have now entered the 2017 Wimbledon tournament!" + "\n" + "\n");
Console.Write("Choose one of the 6 options:" + "\n" + "Press 1 for Default tournament:" + "\n" + "Press 2 for Women's single:" + "\n" +
"Press 3 for Men's single:" + "\n" + "Press 4 for Women's double:" + "\n" + "Press 5 for Men's double:" + "\n" +
"Press 6 for Mix double:" + "\n" + "Insert your choice...: ");

var userValue = Console.ReadKey().KeyChar;
Console.WriteLine();

switch (userValue)
{
case '1':
Console.WriteLine("\n"+ "You have entered a default tournament");
break;
case '2':
Console.WriteLine("\n" + "You have entered women's single");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
break;
case '3':
Console.WriteLine("\n" + "You have entered men's single");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
break;
case '4':
Console.WriteLine("\n" + "You have entered women's double");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
break;
case '5':
Console.WriteLine("\n" + "You have entered men's double");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
break;
case '6':
Console.WriteLine("\n" + "You have entered mix double");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
break;
default:
Console.WriteLine("\n" + "Sorry! You have to choose one of the 6 tournament options");
return false;
}

像这样:……但是好像不行?你们有什么想法吗?

         case '2':
Console.WriteLine("\n" + "You have entered women's single");
Console.Write("Press 1 to start the tournament:" + "\n" + "Press 2 to list the players by first name:" + "\n" +
"Press 3 to list the players by last name:" + "\n" + "Insert your choice...: ");
{
switch (userValue)
{
case '1':
Console.WriteLine("Hello");
break;
}
break;
}

最佳答案

switch 语句本身应该没问题。

问题出在变量 userValue 中。您只评估 var userValue = Console.ReadKey().KeyChar; 一次,因此该值被设置为那个键值。

例如,在您的示例中,外部 switch 语句输入 case '2',因为这是 userValue 设置的内容。但是随后,当进入内部 switch 语句时,它再次使用相同的值,因为 ReadKey() 函数不再被计算。因此,您的 switch 语句会将“userValue”视为 2,甚至根本不会期望或等待任何输入,因此它会从 switch 中掉出来。

在这里编辑:您可以再次调用 ReadKey() 函数来解决这个问题,或者使用第二个变量:

//Inner switch statement:
//{ (bracket is redundant)
userValue = Console.ReadKey().KeyChar;
switch (userValue)
{
// switch body here...
}
//}

关于c# - 在 switch-case 条件中编写 switch-case 条件的可能性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43678212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com