2014-01-10 45 views
0

爲了節省時間,以及我必須清楚傳遞的內容,我將發佈我的整個代碼。這是一個控制檯應用程序,我正在發送一個正在C#中開始的朋友......輸入和我的數組令我感到困惑

問題是,當我從get go中輸入退出時,它退出。但是,讓我說我打進。然後我打了一個數組。打5,或6或任何數字。給我陣列。可以說我想對它進行排序。它排序。但是,如果我點擊EXIT,就說沒有匹配。我不明白。到底是怎麼回事???

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication405 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     // Instantiate the delegate. 
     Del handler = DelegateMethod; 
     string _a = ""; 
     constructor con = new constructor(); 
     bool control = true; 
     while (control) 
     { 
      Console.WriteLine("Enter EXIT to end the program."); 
      Console.WriteLine("Press enter for options"); 
      ConsoleKeyInfo key = Console.ReadKey(); 
      if (key.Key.Equals(ConsoleKey.Enter)) 
      { 
       Console.WriteLine("Enter C for a constructor."); 
       Console.WriteLine("Enter M for a method."); 
       Console.WriteLine("Enter A for an array."); 
       Console.WriteLine("Enter D for a delegate."); 
      } 
      _a = Console.ReadLine(); 
      switch (_a.ToUpper()) 
      { 
       case "EXIT": 
        Console.WriteLine("Thank you for using AJ's program."); 
        control = false; 
        break; 
       case "C": 
        Console.WriteLine(con.a); 
        Console.WriteLine("Would you like to test another scenario?"); 
        _a = Console.ReadLine(); 
        if (_a.ToUpper() == "Y") 
        { 
         continue; 
        } 
        control = false; 
        break; 
       case "M": 
        metroid(); 
        break; 
       case "A": 
        Array(); 
        break; 
       case "D": 
        // call the delegate 
        handler("This is how you call a delegate. Also, Pasta noodles taste like wontons!!! =)"); 
        break; 
       default: 
        Console.WriteLine("No match"); 
        break; 
      } 
     } 
    } 
    public delegate void Del(string message); 
    public static void DelegateMethod(string message) 
    { 
     Console.WriteLine(message); 
    } 
    public class constructor 
    { 
     public string a = "This a is a constructor!"; 
    } 
    static public void metroid() 
    { 
     string b = "This is a method!"; 
     Console.WriteLine(b); 
    } 
    static public void Array() 
    { 
     int temp, k; 
     string ssSize = ""; 
     try 
     { 
      Console.WriteLine("This is a random array. Please enter the size."); 
      string sSize = Console.ReadLine(); 
      int arraySize = Convert.ToInt32(sSize); 
      int[] size = new int[arraySize]; 
      Random rd = new Random(); 
      Console.WriteLine(); 
      for (int i = 0; i < arraySize; i++) 
      { 
       size[i] = rd.Next(arraySize); 
       Console.WriteLine(size[i].ToString()); 
      } 
      Console.WriteLine("Would you like to sort this array?"); 
      ssSize = Console.ReadLine(); 
      if (ssSize.ToUpper() == "Y") 
      { 
       for (int i = 1; i < size.Length; i++) 
       { 
        temp = size[i]; 
        k = i - 1; 
        while (k >= 0 && size[k] > temp) 
        { 
         size[k + 1] = size[k]; 
         k--; 
        } 
        size[k + 1] = temp; 
       } 
       Console.WriteLine("\nThe sorted array is as follows: "); 
       for (int i = 0; i < size.Length; i++) 
       { 
        Console.WriteLine(size[i]); 

       } 
       Console.WriteLine("Note that this uses an insertion sort."); 
      } 

      else 
      { 
       Console.WriteLine("Fine! Don't sort it -- your loss!!!"); 
      } 
     } 
     catch (System.FormatException) 
     { 
      Console.WriteLine("Not correct format, restarting array process."); 
      Array(); 
     } 
    } 
} 
} 

回答

1

這是因爲,當你顯示第一次提示後輸入EXIT,您使用Console.ReadKey()閱讀的第一個字符。因此,_a變量中的值不具有e且僅爲xit

移動顯示的選項是在同一水平休息,問題就會迎刃而解:

static void Main(string[] args) 
    { 
     // Instantiate the delegate. 
     Del handler = DelegateMethod; 
     string _a = ""; 
     constructor con = new constructor(); 
     bool control = true; 
     while (control) 
     { 
      Console.WriteLine("Enter EXIT to end the program."); 
      Console.WriteLine("Enter O for options"); 
      _a = Console.ReadLine(); 
      switch (_a.ToUpper()) 
      { 
       case "EXIT": 
        Console.WriteLine("Thank you for using AJ's program."); 
        control = false; 
        break; 
       case "O": 
        Console.WriteLine("Enter C for a constructor."); 
        Console.WriteLine("Enter M for a method."); 
        Console.WriteLine("Enter A for an array."); 
        Console.WriteLine("Enter D for a delegate."); 
        break; 
       case "C": 
        Console.WriteLine(con.a); 
        Console.WriteLine("Would you like to test another scenario?"); 
        _a = Console.ReadLine(); 
        if (_a.ToUpper() == "Y") 
        { 
         continue; 
        } 
        control = false; 
        break; 
       case "M": 
        metroid(); 
        break; 
       case "A": 
        Array(); 
        break; 
       case "D": 
        // call the delegate 
        handler("This is how you call a delegate. Also, Pasta noodles taste like wontons!!! =)"); 
        break; 
       default: 
        Console.WriteLine("No match"); 
        break; 
      } 
     } 
    } 
+1

謝謝。我不知道昨晚我在想什麼。我只喝了幾杯啤酒,但我仍然應該認識到這一點。再次感謝你。 –

1

您正在使用

ConsoleKeyInfo key = Console.ReadKey(); 

而且它吃行的第一個字符,因爲你沒有打寫命令之前輸入。

最好的解決辦法是用"HELP"代替ENTER,然後堅持Console.Readline

例子:

 while (control) 
     { 
      Console.WriteLine("Enter EXIT to end the program, HELP for options"); 
      _a = Console.ReadLine(); 
      switch (_a.ToUpper()) 
      { 
       case "HELP": 
        Console.WriteLine("Enter C for a constructor."); 
        Console.WriteLine("Enter M for a method."); 
        Console.WriteLine("Enter A for an array."); 
        Console.WriteLine("Enter D for a delegate."); 
        break; 
       case "EXIT": 
       .... 
       ...