2013-07-20 27 views
1

我很新的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() 
    { 

    } 
} 
+1

你在'menu'中除了'0'之外什麼都不做,所以你的'switch'永遠不會匹配任何東西。你不需要在任何地方創建一個「Choice」類的實例,而「Numbers」和「Words」是該類的方法。恐怕你的問題有很多問題需要回答。你需要看看你的代碼在做什麼,以及它應該做什麼,並試圖讓這兩件事情相匹配。 :-) –

+0

你可以作弊,讓VS把功能放在正確的位置 - 選擇你想要在功能中的代碼,並從右鍵菜單中選擇「提取功能」(我相信它在VS的所有版本中都可用)。 。 –

+0

Marietjie,你會澄清案件1是否應該在控制檯輸入大量數字的人?或者每個循環迭代只有一個數字? –

回答

0

不能使用的方法在Main中的Choice類中定義,因爲您尚未使用public標識符聲明它們。在C#中,類屬性默認爲private,所以除非您明確聲明它們爲public,否則只有類本身才會知道它們的存在。

所以基本上只是從static void MethodName改變所有聲明的Choicepublic static void MethodName,然後你就可以從Choice類一樣叫他們主要的;

Choice.Exit(); 

編輯:你還需要做一些改變,使switch語句工作。正如在評論中指出的那樣,menu的值不是0。我建議你使用更類似於以下的東西;

 isValid = true; 
     int menu = 0; 
     int number; 
     string word; 

     Console.WriteLine("What type do you want to use?"); 
     Console.WriteLine("Press 1 for numbers, 2 for words, or 3 exit."); 
     string input = Console.ReadLine(); // we must read the users input 

     if (!int.TryParse(input, out menu)) 
     { 
      // the user didn't enter a number make them try again 
      // note you might want to use a loop here to ensure the program does not 
      // proceed until the user has entered "1", "2", or "3" 
     } 

     switch (menu) 
+1

謝謝:) @evanmcdonnal – DavMar

+1

@MarietjieDavel,你可以按'接受'按鈕來承認埃文的答案對你有幫助 –

+0

@MarietjieDavel你非常歡迎。祝功績好。 – evanmcdonnal

0

如果我理解你的問題,只是把你的功能在你的班級裏。我相應地調整了您的代碼。您的單詞/數字類也可能有問題。您通常必須實例化它們,但是使用類似Choice myChoice = new Choice(); 類節目 { 枚舉菜單 { 編號= 1, 字= 2, 退出= 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); 
       int retInt = functionGetInt(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); 
       string retString = functionGetString(word); 
       break; 

      case Menu.Exit: 

       break; 
      default: 
       Console.WriteLine("You have made an invalid selection, try again"); 
       isValid = false; 
       break; 
     } 
    } while (isValid); 
private string functionGetString(string pParmString) 
{ 
//code 
return "string"; 
} 
private int functionGetInt(int pParmInt) 
{ 

//code 
return 0; 
} 

}

}

+0

此代碼只能達到默認情況! –

0

編輯:

對於您提出的在這裏,這將是一個possibility.I取出枚舉,我通常試圖解決提供的代碼的代碼,但有沒有必要:

class Program 
    { 
     //enum Menu 
     //{ 
     // Numbers = 1, 
     // Words = 2, 
     // Exit = 3, 
     //} 

     static void Main(string[] args) 
     { 
      bool isValid; 
      do 
      { 
       isValid = true; 
       int 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 "); 

       string s = Console.ReadLine(); 
       while (!Regex.IsMatch(s, "^[1-3]{1}$")) 
       { 
        Console.WriteLine("Please enter a valid choice(1 to 3)"); 
        s = Console.ReadLine(); 
       } 
       menu = Convert.ToInt32(s); 
       switch (menu) 
       { 
        case 1: 
         List<int> numberarr = new List<int>(); 
         Console.WriteLine("Please input as many numbers as you like separeted by space or comma,or type exit"); 
         string numbers = Console.ReadLine(); 
         if (numbers == "exit") 
          Choice.Exit(); 
         else 
         { 
          number = numbers.Split(new char[] { ',', ' ' }).Select(x => int.Parse(x)).ToArray(); 
          numberarr.AddRange(number); 
          Choice.Numbers(numberarr.Sum(), numberarr.Count, numberarr.Average(), numberarr.Max(), numberarr.Min()); 
         } 

         break; 
        case 2: 
         List<string> wordarr = new List<string>(); 
         Console.WriteLine("Please input as many numbers as you like separeted by space or comma"); 
         word = Console.ReadLine(); 
         wordarr.AddRange(word.Split(new char[] { ',', ' ' })); 
         Choice.Words(wordarr); 
         break; 
        case 3: 
         Choice.Exit(); 
         break; 
        default: 
         Console.WriteLine("You have made an invalid selection, try again"); 
         isValid = false; 
         break; 
       } 
      } while (isValid); 
      Console.ReadKey(); 
     } 
    } 

    class Choice 
    { 
     public static void Numbers(int sum, int count, double average, int max, int min) 
     { 
      int a = sum; 
      int b = count; 
      double c = average; 
      int d = max; 
      int e = min; 
      //just as example. 
     } 

     public static void Words(List<string> args) 
     { 
      //do whatever you need here 
     } 
     public static void Exit() 
     { 
      Environment.Exit(0); 
     } 
    } 
+0

這是一個正確的方向,但它會拋出異常。如果Convert.Int32的結果不是1,2或3,並且如果輸入不是整數,Convert.Int32會拋出異常。 – evanmcdonnal

+0

是的,我知道evanmcdonald,只是編輯大聲笑:)謝謝:) – terrybozzio

+0

您所做的編輯沒有解決這個問題。對她來說,這可能是足夠的,但在企業軟件或任何面向客戶的軟件中這是不可接受的。輸入驗證是必需的。 – evanmcdonnal

相關問題