2014-04-02 67 views
0

我正在構建基於控制檯的彩票遊戲,用戶輸入他/她的數字選擇。我需要檢查數字是在1-39之間,用戶輸入了7個有效數字。我想以用戶在控制檯中將它們寫入一行字符串的方式執行此操作,並且程序找到任何空格,逗號或其他非數字字符並忽略它們。其餘的彩票號碼應存儲在整數數組中,不包含任何重複項。我想從字符串搜索整數並將它們存儲在數組中

我目前的版本非常糟糕,因爲如果用戶編寫例如數字之間有2個空格。

Console.WriteLine("Choose 7 numbers (1-39) by pressing ENTER after every number:"); 
     string userRow = Console.ReadLine(); 
     int[] userLottery = new int[7]; 
     for(int i = 0; i < userLottery.Length; i++) 
      { 
       userLottery = userRow.Split(',', '.', ' ').Select(x => int.Parse(x)).ToArray(); 
       if(userLottery[i] > 7) 
       { 
        Array.Resize(ref userLottery, 7); 
       } 
      } 

我想以更方便的方式替換目前的方式,其中用戶錯誤的數量不會影響程序。如果用戶寫入多個空格,則會發生錯誤。

我試圖建立正則表達式來處理這些情況,但我不能用它來將它們存儲到數組中。

string userChoice = Console.ReadLine(); 
     MatchCollection userNumbers = Regex.Matches(userChoice, @"\d+"); 
     int[] userRow; 

     for(int i = 0; i < userNumbers.Count; i++) 
     { 
      userRow[i] = userNumbers[i].Value; 
     } 

,說字符串不能轉換成int [] ...

+0

所以它的工作原理,但脆:做什麼,你告訴用戶下面?什麼是問題? –

+0

你的具體問題是什麼? – Coops

+0

我想以更方便的方式替換我目前的用戶錯誤數量不會影響程序的方式。如果用戶寫入多個空格,則會發生錯誤。並 – johnnyCasual

回答

0

您的代碼與用戶的指令不符。

Console.WriteLine("Choose 7 numbers (1-39) by pressing ENTER after every number:"); 
    int[] userLottery = new int[7]; 

    int i = 0; 
    while (i < 7) 
    { 
     Console.Write("Your choice #{0}: ", i+1); 
     string userRow = Console.ReadLine(); 
     int userNumber; 

     if (!Int32.TryParse(userRow, out userNumber) || userNumber < 1 || userNumber > 39) 
     { 
      Console.WriteLine("Invalid number! Please try again!"); 
     } 
     else 
     { 
      userLottery[i++] = userNumber; 
     } 
    } 
+0

我真的很喜歡這個!我加了一個 - 如果要測試數組是否已經存在於數組中,所以沒有任何重複 – johnnyCasual

0

使用Regex.Split()這個處理多個空格。

string[] numbers = Regex.Split(userRow, @"\s+"); 

\s+表示一個或多個空格。如果需要,您只能使用[ ]+

1

你可以使用使用String.Split這個LINQ查詢與RemoveEmptyEntriesint.tryParse

int num = 0; 
int[] userLottery = userRow.Trim() 
    .Split(new[] { '.', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries) 
    .Where(s => int.TryParse(s.Trim(), out num) && num > 0 && num < 40) 
    .Select(s => num) 
    .Distinct() 
    .ToArray(); 
if(userLottery.Length != 7) 
    Console.WriteLine("Enter 7 valid numbers between 1 and 39"); 
else 
    Console.WriteLine("You have chosen following numbers: " + string.Join(",", userLottery)); 

Enumerable.Distinct刪除重複的要求。

+0

謝謝!得到它的工作:)! – johnnyCasual

0

你可以先用RegEx解析它,然後它就可以工作。

using System.Text.RegularExpressions; 

    Console.WriteLine("Choose 7 numbers (1-39) by pressing ENTER after every number:"); 
    string userRow = Console.ReadLine(); 
    int[] userLottery = new int[7]; 

    string[] userEnter = Regex.Split(userRow, " "); 

    int n = 0; 
    int k = 0; 
    for (int i = 0; i < userEnter.Length; i++) 
    { 
    bool isNumeric = int.TryParse(userEnter[i], out n); 
    if(isNumeric == true) 
    { 
     userLottery[k] = int.Parse(userEnter[i]); 
     k++; 
    } 
    } 
0

使用下面的代碼

 Console.WriteLine("Choose 7 numbers (1-39) by pressing ENTER after every number:"); 

     string data = Console.ReadLine(); 

     int[] userLottery = new int[7]; 
     int i = 0; 

     StringBuilder num = new StringBuilder(); 
     foreach (char item in data) 
     { 


      if (!Char.IsDigit(item)) 
      { 
       if (num.Length == 0) 
        continue; 
       userLottery[i] = int.Parse(num.ToString()); 
       i++; 
       num.Clear(); 
       continue; 
      } 

      num.Append(item); 
     } 

     if(num.Length > 0) 
      userLottery[i] = int.Parse(num.ToString()); 
相關問題