我正在使用菜单,我想在菜单选项中使用", "
和let array = [1,2,3,4]
let string = array.map(String.init).joined(separator: ", ") // "1, 2, 3, 4"
。我也想循环这个程序,直到用户决定退出(通过选择菜单中的最后一个选项)。
我被卡住了,因为我不知道如何进行循环,或者switch
用于第一个菜单选项。我希望用户能够在包里添加任意数量的东西,比如“猫”,“狗”,“汽车”等。
这就是我的代码现在的样子:
list
答案 0 :(得分:0)
我要做的是:
mainCompartment
和outerCompartment
列表。while
循环中包含您的选择选项,以便您可以继续提问,直到用户退出。这将需要某种类型的标志,因此我们知道何时退出,因此创建一个名为allDone
的布尔值,以false
开头,如果用户选择选项4则将其设置为true
。 int.TryParse
的结果,因为如果输入不是'它将返回false。 t一个int,并将其与边界结合起来检查该数字是否为1-4。switch
语句中调用它们。这将使您的主体代码更容易阅读。以下是完成后主代码的外观:
private static void Main()
{
Console.Title = "5";
Console.ForegroundColor = ConsoleColor.Blue;
List<string> outerCompartment = new List<string>();
List<string> mainCompartment = new List<string>();
bool allDone = false;
while (!allDone)
{
Console.WriteLine("\nThis is your bag!");
Console.WriteLine("[1] to pack things in the main compartment");
Console.WriteLine("[2] to pack things in the outer compartment");
Console.WriteLine("[3] to see packed things");
Console.WriteLine("[4] to quit");
Console.WriteLine("Please enter your choice: ");
int choice;
while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)
{
Console.WriteLine("Invalid input. Enter a number from 1 to 4: ");
}
switch (choice)
{
case 1:
GetItemsAndAddToCompartment(mainCompartment, "main");
Console.Clear();
break;
case 2:
GetItemsAndAddToCompartment(outerCompartment, "outer");
Console.Clear();
break;
case 3:
DisplayCompartmentContents(mainCompartment, "Main");
DisplayCompartmentContents(outerCompartment, "Outer");
break;
case 4:
Console.WriteLine("All done. Have a great trip!");
allDone = true;
break;
}
}
Console.WriteLine("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
主代码调用的两个辅助函数:
static void GetItemsAndAddToCompartment(List<string> compartment, string compartmentName)
{
if (compartment == null) throw new ArgumentNullException(nameof(compartment));
Console.WriteLine($"Enter items to add to {compartmentName} compartment below. Type 'done' when finished.");
int counter = 1;
while (true)
{
Console.Write($"Enter item #{counter++}: ");
string item = Console.ReadLine();
if (item.Equals("done", StringComparison.OrdinalIgnoreCase)) break;
compartment.Add(item);
}
}
static void DisplayCompartmentContents(List<string> compartment, string compartmentName)
{
Console.WriteLine($"{compartmentName} compartment contents");
Console.WriteLine("-------------------------");
if (compartment == null || !compartment.Any())
{
Console.WriteLine("[No items in this compartment]");
}
else
{
compartment.ForEach(Console.WriteLine);
}
}
答案 1 :(得分:0)
试试这个:
class Program
{
static void Main(string[] args)
{
PrintMenu();
List<string> lBag = new List<string>();
bool bQuit = false;
int iChoice = -1;
string sIn = string.Empty;
while (!bQuit)
{
sIn = Console.ReadLine();
if (!Int32.TryParse(sIn, out iChoice) || !(iChoice >= 1 && iChoice <= 3))
{
Console.WriteLine("\t Invalid input. Try again:");
PrintMenu();
continue;
}
switch (iChoice)
{
case 1:
Console.WriteLine("\t Insert the item you want to add:");
lBag.Add(Console.ReadLine());
Console.WriteLine("\t Item added successfully.");
PrintMenu();
break;
case 2:
Console.WriteLine(string.Format("\t Current bag: [{0}]\n", string.Join(", ", lBag)));
PrintMenu();
break;
case 3:
Console.WriteLine("\t Quitting...");
bQuit = true;
break;
default:
break;
}
}
}
static void PrintMenu()
{
Console.WriteLine("\n Please choose one of the options below:");
Console.WriteLine("\t [1] Add item to bag");
Console.WriteLine("\t [2] Display the bag");
Console.WriteLine("\t [3] Quit");
}
}
答案 2 :(得分:-4)
真正的答案:使用按钮和弹出窗口创建UI; - )
这个修复程序会给我带来很多挫折(警告:'这个解决方案被视为有害......')
class Program
{
static void Main(string[] args)
{
Console.Title = "5";
Console.ForegroundColor = ConsoleColor.Blue;
// ________________________________________________________
loop:
Console.WriteLine("\n \t This is your bag!");
Console.WriteLine("\t [1] to pack things");
Console.WriteLine("\t [2] to pack things in the outercompartment");
Console.WriteLine("\t [3] to see packed things");
Console.WriteLine("\t [4] to quit");
Console.WriteLine("\t your choice: ");
string str = Console.ReadLine();
int nr = Convert.ToInt32(str);
List<string> items = new List<string>();
items.Add(str);
switch (nr)
{
case 1:
packing:
Console.Write("What would you like to pack? [QUIT for menu]\t");
str = Console.ReadLine();
if (str=="QUIT") goto loop;
items.add(str);
Console.WriteLine("You packed a " + str);
goto packing;
break;
case 4:
goto quitloop;
}
Console.ReadKey();
goto loop;
}
quitloop:
}
当然没有经过测试。
这也是一个可怕的懒人修复......你可以转换这个做,等等。