2012-01-27 39 views
0

我在c#中完成的方法有一些問題。我試圖阻止用戶輸入任何東西,然後Y和N。它幾乎是我想要的,但用戶仍然可以輸入多個符號,然後它不起作用!我該怎麼做才能檢查char是否超過一個字符?我認爲tryParse解決了這個問題?謝謝!Char tryParse沒有按照我的想法工作?

// Method to check if item is food or not 
    private void ReadIfFoodItem() 
    { 
     Console.Write("Enter if food item or not (y/n): "); 

     if (char.TryParse(Console.ReadLine(), out responseFoodItem)) 
     { 
      if(Char.IsNumber(responseFoodItem)) 
      { 
       Console.WriteLine(errorMessage); 
       ReadIfFoodItem(); 
      } 
      else 
      { 
       // Set true or false to variable depending on the response 
       if ((responseFoodItem == 'y' || responseFoodItem == 'Y')) 
       { 
        foodItem = true; 
        selectedVATRate = 12; // Extra variable to store type of VAT 
       } 
       else if ((responseFoodItem == 'n' || responseFoodItem == 'N')) 
       { 
        foodItem = false; 
        selectedVATRate = 25; // Extra variable to store type of VAT 
       } 
       else 
       { 
        Console.WriteLine(errorMessage); 
        ReadIfFoodItem(); 
       } 
      } 
     } 
    } 
+3

你當然希望有很多! – leppie 2012-01-27 09:58:13

+1

如果你想限制用戶輸入1個字符,你不能只使用'Console.Read()'? – annonymously 2012-01-27 10:00:46

+0

也許我應該補充一點,我是C#的新手,這個解決方案並不是最好的! :) – 2012-01-27 10:02:31

回答

1

下面的代碼「起作用」,它產生了預期的結果。

 char responseFoodItem; 

     Console.Write("Enter if food item or not (y/n): "); 

     if (char.TryParse(Console.ReadLine(), out responseFoodItem)) 
     { 
      // Set true or false to variable depending on the response 
      if ((responseFoodItem == 'y' || responseFoodItem == 'Y')) 
      { 
       Console.WriteLine("foodItem = true"); 
      } 
      else if ((responseFoodItem == 'n' || responseFoodItem == 'N')) 
      { 
       Console.WriteLine("foodItem = false"); 
      } 
      else 
      { 
       Console.WriteLine("Unrecognised input"); 
      } 
     } 
     else 
     { 
      Console.WriteLine("Invalid input"); 
     } 

然而,有其他人指出使用ReadKey是一個更好的解決方案,如果你想輸入限制爲單個鍵。這也意味着用戶不必按回車鍵就可以接受輸入。

0

char.TryParse只是試圖解析作爲參數提供的字符串,它不限制,你可以輸入到控制檯中使用Console.ReadLine的字符數。

當用戶輸入多個單個字符時,char.TryParse將失敗,因爲由Console.ReadLine返回的字符串不包含單個字符。

您應該改用Console.Read

+0

嗨,我正在嘗試使用Console.Read的各種組合,但我總是得到恩ren強調?什麼可能是錯的? – 2012-01-27 10:50:35

0

我該怎麼做也檢查是否char多於一個字符?

string line = Console.ReadLIne(); 
If(!string.IsNullOrEmpty(line) && line.Length > 1) 

用於讀取字符使用Console.ReadChar()代替。

0

Console.ReadLine允許用戶輸入任意長度的字符串並按下回車鍵。

我該怎麼做還要檢查char是否超過一個字符?我認爲tryParse解決了這個問題?

the manual page

指定字符串爲其等效的Unicode字符的值轉換。返回代碼指示轉換是否成功還是失敗....如果s參數爲空或S的長度不轉換失敗1.

您是否嘗試過使用Console.ReadKey代替的ReadLine ?

0

要檢查它的用戶已經將一個以上的字符,你可以檢查字符串的長度,而不是使用Char.TryParse

...... 
private void ReadIfFoodItem() 
{ 
string answer=string.empty; 
Console.Write("Enter if food item or not (y/n): "); 
answer=Console.ReadLine() 
if (answer.lenght>=1)) 
    { 
      //error 
      ....... 
    } 

...............

0

使用Console.ReadKey()來限制字符數量。要測試你是否有Y或N,那麼你可以比較ASCII代碼或使用正則表達式。

+0

當我剛剛使用Console.Read()時,我在代碼下面看到一條紅線! – 2012-01-27 10:07:11

+0

對不起本來是ReadKey – CSharpened 2012-01-27 10:34:12

1

char代表一個字符,所以How can I do to also check if char is more than one char? I thought the tryParse solved that?似乎有點無厘頭... TryParse會嘗試從您的輸入解析單個字符,如果該值是null或具有長度將明確地失敗> 1.

而不是檢查一個字符,只需檢查字符串,例如:

string line = Console.ReadLine(); 
switch (line.ToUpperInvariant()) 
{ 
    case "Y": 
     // Do work for y/Y 
     break; 
    case "N": 
     // Do work for n/N 
     break; 
    default: 
     // Show error. 
     break; 
} 
0

您可以嘗試使用Console.ReadKey 這只是用戶的一個按鍵,並且您知道不會有多個字符。

0

爲什麼不比較輸入的字符串?
爲什麼不簡化比較?

using System.Linq; 

private static string[] ValidAnswers = new string[]{ "y", "yes" }; 

// Method to check if item is food or not 
private void ReadIfFoodItem() { 
    Console.Write("Enter if food item or not (y/n): "); 
    string ans = Console.ReadLine(); 
    // Checks if the answer matches any of the valid ones, ignoring case. 
    if (PositiveAnswers.Any(a => string.Compare(a, ans, true) == 0)) { 
     foodItem = true; 
     selectedVATRate = 12; // Extra variable to store type of VAT 
    } else { 
     foodItem = false; 
     selectedVATRate = 25; // Extra variable to store type of VAT 
    } 
} 

或者,正如其他人所說,您可以使用Console.ReadKey()

相關問題