C# 我希望用户键入“4”,然后让程序重复我在程序中前面的代码
这是我的控制台程序中的代码片段:C# 我希望用户键入“4”,然后让程序重复我在程序中前面的代码,c#,C#,这是我的控制台程序中的代码片段: Console.WriteLine("1.) Tom"); Console.WriteLine("2.) Lucas"); Console.WriteLine("3.) Sarah"); Console.WriteLine("4.) Back"); var userChoice = Convert.ToInt32(Console.ReadLine()); switch (userChoice){ case "4": break; } 所以我
Console.WriteLine("1.) Tom");
Console.WriteLine("2.) Lucas");
Console.WriteLine("3.) Sarah");
Console.WriteLine("4.) Back");
var userChoice = Convert.ToInt32(Console.ReadLine());
switch (userChoice){
case "4":
break;
}
所以我只是想找到一种方法,我可以在程序的前面重复一行代码。如果你有任何想法,请分享。我试图重复的代码行与此类似,只是选项:学生、荣誉榜、退出。这是选项的函数
public void function1 ()
{
Console.WriteLine("1.) Tom");
Console.WriteLine("2.) Lucas");
Console.WriteLine("3.) Sarah");
Console.WriteLine("4.) Back");
}
然后你可以用这个来调用它
var userChoice = Convert.ToInt32(Console.ReadLine());
switch (userChoice)
{
case "4":
function1;
break;
}
这应该行得通
var userChoice = Convert.ToInt32(Console.ReadLine());
switch (userChoice){
case "4":
Console.WriteLine("4.) Back");
break;
}
你必须先设计好。您可以将每个部分划分为多个方法,并根据输入调用该方法:。