我很新的C#編程,我有一個問題。我不知道在哪裏放置我的函數,以及如何聲明它們,以便我可以從我的switch語句中調用它們。是否我能夠用我的numberarr和wordarr陣列在我的功能或者我還需要創建一個單獨的功能呢?這裏是我的代碼:把我的函數放在哪裏,以便可以從我的switch語句中調用它?
class Program
{
enum Menu
{
Numbers = 1,
Words = 2,
Exit = 3,
}
static void Main(string[] args)
{
bool isValid;
do
{
isValid = true;
Menu menu = 0;
int number;
string word;
Console.WriteLine("Choose an option from the menu: ");
Console.WriteLine("1. Numbers ");
Console.WriteLine("2. Words ");
Console.WriteLine("3. Exit ");
switch (menu)
{
case Menu.Numbers:
List<int> numberarr = new List<int>();
Console.WriteLine("Please input as many numbers as you like or type exit");
number = int.Parse(Console.ReadLine());
numberarr.Add(number);
break;
case Menu.Words:
List<string> wordarr = new List<string>();
Console.WriteLine("Please input as many numbers as you like");
word = Console.ReadLine();
wordarr.Add(word);
break;
case Menu.Exit:
break;
default:
Console.WriteLine("You have made an invalid selection, try again");
isValid = false;
break;
}
} while (isValid);
}
}
class Choice
{
static void Numbers(int sum, int count, int average, int max, int min)
{
}
static void Words(string[] args)
{
}
static void Exit()
{
}
}
你在'menu'中除了'0'之外什麼都不做,所以你的'switch'永遠不會匹配任何東西。你不需要在任何地方創建一個「Choice」類的實例,而「Numbers」和「Words」是該類的方法。恐怕你的問題有很多問題需要回答。你需要看看你的代碼在做什麼,以及它應該做什麼,並試圖讓這兩件事情相匹配。 :-) –
你可以作弊,讓VS把功能放在正確的位置 - 選擇你想要在功能中的代碼,並從右鍵菜單中選擇「提取功能」(我相信它在VS的所有版本中都可用)。 。 –
Marietjie,你會澄清案件1是否應該在控制檯輸入大量數字的人?或者每個循環迭代只有一個數字? –