2014-03-12 176 views
1
public static void readkey() 
{ 
    ConsoleKeyInfo input = Console.ReadKey(); 

    switch(input.Key) 
    { 
     case ConsoleKey.D9: 
      Console.Clear(); 
      Console.WriteLine("U heeft het programma succesvol afgesloten!"); 
      Console.Read(); 
      Environment.Exit(0); 
     break; 

     case ConsoleKey.D1: 
      overzichtmp(); 
     break; 

     case ConsoleKey.D2: 
      overzichtvr(); 
     break; 

     default: 
     { 
      Console.Clear(); 
      Console.WriteLine("Voer een geldig nummer in!"); 
      Console.Read(); 
      showmenu(); 
      break; 
     } 
    } 
} 

如果我第一次按一個鍵,它閃爍的方法,直接回來,第二次它只是開始正常任何想法如何解決這個問題?ReadKey需要輸入兩次

+0

您是否使用Visual Studio?您可以使用調試器逐行瀏覽代碼,並查看到底發生了什麼。 – mason

+0

是的,但我不知道是什麼導致它 – Script99

+1

我認爲'Console.Read'期待第二個輸入。 –

回答

0

我不確定下面是你正在尋找的,但它設置了程序循環來調用你的讀寫鍵,並消除第二個Console.Read並用睡眠等待來替換它。

static void Main(string[] args) 
{ 
    while (true) 
    { 
     readkey(); 
    } 
} 

public static void readkey() 
{ 
    ConsoleKeyInfo input = Console.ReadKey(); 

    switch(input.Key) 
    { 
     case ConsoleKey.D9: 
      Console.Clear(); 
      Console.WriteLine("U heeft het programma succesvol afgesloten!"); 
      //You might want to wait: System.Threading.Thread.Sleep(5000); 
      Environment.Exit(0); 
     break; 

     case ConsoleKey.D1: 
      overzichtmp(); 
     break; 

     case ConsoleKey.D2: 
      overzichtvr(); 
     break; 

     default: 
     { 
      Console.Clear(); 
      Console.WriteLine("Voer een geldig nummer in!"); 
      // possibly add the wait here as well 
      showmenu(); 
      break; 
     } 
    } 
}