2 回答

TA貢獻1886條經驗 獲得超2個贊
有兩件事可以解決你的困難。
首先,輸入提示應該位于循環(huán)中,以便它可以顯示每個循環(huán)何時開始。
其次,do while
循環(huán)決定循環(huán)是否應該在設計的最后繼續(xù);這取決于應直接用作 while 條件的用戶輸入。
因此,您的代碼可以簡化為
public static void Main()
{
List<string> names = new List<string>() { "Vasti", "Cameron", "Ezra", "Tilly" };
string userChoice = "";
do
{
Console.WriteLine($@"Welcome to the names lost!{Environment.NewLine} If you wish to add a name to the list, type 1.{Environment.NewLine} If you want to see current names in the list, type 2.");
userChoice = Console.ReadLine();
switch (userChoice)
{
case "1":
Console.WriteLine("Add a name to the squad.");
string userAddName = Console.ReadLine();
names.Add(userAddName);
break;
case "2":
Console.WriteLine("Here's the list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
break;
default:
Console.WriteLine("Please type either 1 or 2 to select an option.");
break;
}
Console.WriteLine(@"Wanna do that again? Type yes or no.");
userChoice = Console.ReadLine();
}
while (userChoice.Equals("yes", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("Program finished");
Console.ReadLine();
}

TA貢獻1824條經驗 獲得超5個贊
將 do{}while() 循環(huán)更改為常規(guī) while(){} 循環(huán)。Move string userChoice = Cosole.ReadLine(); 在頂部 while 循環(huán)內部,否則您將永遠不會要求其他選項。。。還要在開頭設置 ((bool program=true; 和 bool program=true;))。Do{}while() 循環(huán)首先執(zhí)行 {} 內的所有操作,然后檢查 while 語句是否仍然為 true。
- 2 回答
- 0 關注
- 152 瀏覽
添加回答
舉報