2017-10-20 188 views
0

我是C#的新手,但我知道Java相當不錯。我可能錯過了一些簡單的東西,因爲剛剛接觸這種特定的語言,我只是不知道。我試圖讓一個while循環重複直到用戶選擇Q.循環退出正常,但是它的代碼在它停止之前重複3次以提示用戶進行選擇。爲什麼會發生?因爲使用的是Console.Read(),這將處理輸入流中的下一個字符,它包括從當用戶按下Enter\n\r字符C#我的while循環重複3次,然後它提示另一個選擇

class MainClass 
{ 


    public static void DisplayMenu() { 
     Console.WriteLine("What would you like to do?"); 
     Console.WriteLine("(D)eposit"); 
     Console.WriteLine("(W)ithdraw"); 
     Console.WriteLine("(C)alculateInterest"); 
     Console.WriteLine("(S)howBalance"); 
     Console.WriteLine("(Q)uit"); 
     Console.WriteLine("************************************"); 
     Console.WriteLine("Make choice by entering first letter of choice,"); 
     Console.WriteLine("then press ENTER key:"); 
    } 
    static void Main(string[] args) { 

     Account account = new Account(); 
     char choice; 
     double amount = 0.0; 

     Console.WriteLine("************************************"); 
     Console.WriteLine("Welcome to Bernard's Bodacious Bank!"); 
     Console.WriteLine("************************************"); 
     Console.WriteLine("We have opened your account"); 
     DisplayMenu(); 
     choice = Char.ToUpper((char)Console.Read()); 

     while (!choice.Equals('Q')) { 

      DisplayMenu(); 
      choice = Char.ToUpper((char)Console.Read()); 

     } 

     account.ShowTransactions(); 
     Console.ReadKey(); 
    } 
} 
+0

可能被拋出某處未處理的異常。您可以添加一個try/catch塊或附加您的調試器,並確保它捕獲所有clr異常。 – Igor

+0

如果你告訴他們按回車你應該使用Console.ReadLine()。您也可以在重新繪製菜單後按下Enter鍵來清除屏幕。 – numbtongue

回答

1

原因是可能的。這些字符會立即在呼叫Console.Read()的循環內處理。

如果你只是想要一個字符,你可以改爲使用Console.ReadKey(),它返回用戶鍵入的第一個鍵(類型ConsoleKeyInfo)。然後,你可以做一個比較,它是Key屬性,如:

Console.WriteLine("************************************"); 
Console.WriteLine("Welcome to Bernard's Bodacious Bank!"); 
Console.WriteLine("************************************"); 
Console.WriteLine("We have opened your account"); 

double amount = 0.0; 
ConsoleKeyInfo choice; 

do 
{ 
    DisplayMenu(); 
    choice = Console.ReadKey(); 
} while (choice.Key != ConsoleKey.Q); 

另外,如果你DO要允許他們按回車鍵,那麼你應該使用Console.ReadLine()方法和檢查,看看是什麼StartsWith

DisplayMenu(); 
string choice = Console.ReadLine(); 

while (!choice.StartsWith("Q", StringComparison.OrdinalIgnoreCase)) 
{ 
    DisplayMenu(); 
    choice = Console.ReadLine(); 
} 

這裏有一個如何處理輸入的例子。它假定你有一個可以要求每個選擇方法:

var quit = false; 

while(!quit) 
{ 
    DisplayMenu(); 

    var choice = Console.ReadKey(); 

    switch (choice.Key) 
    { 
     case ConsoleKey.D: 
      Deposit(); 
      break; 
     case ConsoleKey.W: 
      Withdrawl(); 
      break; 
     case ConsoleKey.C: 
      CalcInterest(); 
      break; 
     case ConsoleKey.S: 
      ShowBalance(); 
      break; 
     case ConsoleKey.Q: 
      quit = true; 
      break; 
     default: 
      Console.WriteLine("Invalid entry, try again"); 
      break; 
    } 
} 
+0

我不認爲這種說法是正確的:「原因可能是因爲您使用的是Console.Read(),它將返回整行......」Console.Read()的MSDN文檔說它返回下一個來自輸入流的字符。是的,它會阻止,直到用戶點擊Enter,但每個呼叫只會返回一個字符,而不是整行。 – adv12

+0

你說得對,讓我試着糾正這一點。整行成爲輸入流的一部分,但是「Read()」只處理一個字符。 –

+0

@ adv12更新,謝謝! –

1

從MSDN文檔:

Read方法阻斷其回報,而你輸入的輸入字符;當您按下Enter鍵時它會終止。按Enter鍵將依賴於平臺的行終止序列添加到您的輸入中(例如,Windows追加回車換行符序列)。隨後對Read方法的調用一次檢索您的輸入一個字符。讀取最後一個字符後,Read再次阻止其返回,並重復該循環。

在Windows中,「依賴於平臺的行終止序列」等於\r\n。所以當你輸入A,然後回車,你實際上發送了3個擊鍵讓你的程序處理。

另外,從MSDN文檔:

ReadLine方法,或KeyAvailable屬性和ReadKey方法是優選使用的讀取方法。

0

這似乎是最簡單的答案,使用讀取鍵的方法,然後使用choice.Key和ConsoleKey.Q。感謝你們的幫助!它指出我在正確的方向!

靜態無效的主要(字符串[]參數){

 Account account = new Account(); 
     double amount = 0.0; 
     ConsoleKeyInfo choice; 

     Console.WriteLine("************************************"); 
     Console.WriteLine("Welcome to Bernard's Bodacious Bank!"); 
     Console.WriteLine("************************************"); 
     Console.WriteLine("We have opened your account"); 
     DisplayMenu(); 
     choice = Console.ReadKey(); 
     while (choice.Key != ConsoleKey.Q) { 
      switch (choice.Key) { 
       case ConsoleKey.D: 
        Console.WriteLine("found d!"); 
        break; 
       default: 
        Console.WriteLine("Input Error!"); 
        break; 
      } 
     } 

     account.ShowTransactions(); 
     Console.ReadKey(); 
    } 
相關問題