2013-11-21 94 views
0

我想通過用戶輸入讀取字符串並將其存儲在數組中。該字符串必須是由空格分隔的單個數字數字。用戶將輸入完全20個數字,並且連續兩次不會出現數字。讀取一個字符串,將其分開,並將其放入一個數組中C#

例字符串:

1 2 9 6 3 2 4 5 8 1 3 6 4 7 8 2 1 9 6 3 

下面是代碼我必須這樣做,以及錯誤檢查和它不能正常工作。我想我正在考慮錯誤檢查。在下面的代碼中,refString是大小爲20的int數組。

case 2: 
bool validated = false; 
Console.WriteLine("\nPlease enter a 20 character reference string, each separated by a single space"); 
Console.WriteLine("A number should not occur twice in a row, ex: 1 5 4 4 6"); 
Console.WriteLine("Each character must be an integer 1-9"); 
Console.WriteLine("Example reference string: 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6"); 
Console.WriteLine("\nEnter reference string: "); 
string s = Console.ReadLine(); 
refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); //split string and add numbers into array 
int totalCount = refString.Length; 

//if the user entered exactly 20 numbers, check to see if any of the numbers occur twice in a row 
if (totalCount == 20) 
{ 
    for (i = 1; i < 20; i++) 
    { 
     while (refString[i] == refString[i - 1]) //make sure two of the same number side by side do not occur 
     { 
      break; 
     } 

    } 
} 

while (totalCount != 20 || !validated) 
{ 
    for (i = 1; i < 20; i++) 
    { 
     while (refString[i] == refString[i - 1]) //make sure two of the same number side by side do not occur 
     { 
      Console.WriteLine("\nError: reference string is NOT 20 numbers"); 
      Console.WriteLine("\nEnter reference string: "); 
      s = Console.ReadLine(); 
      refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); 
      totalCount = refString.Length; 
     } 
    } 
    if (totalCount == 20) 
    { 
     for (i = 1; i < 20; i++) 
     { 
      while (refString[i] == refString[i - 1]) //make sure two of the same number side by side do not occur 
      { 
       break; 
      } 
     } 
    } 

} 

break; 
} 

回答

0

您可以使用正則表達式來檢查是否有兩個連續重複的數字

if(!Regex.IsMatch(n,@".*(\d) \1.*")) 
    s.Split(" ").Select(x=>int.Parse(x)); 
0
bool correct = false; 
while(!correct){ 
    // ask for numbers until correct 
    Console.WriteLine("\nEnter reference string: "); 
    string s = Console.ReadLine(); 
    refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); 
    int totalCount = refString.Length; 

    // check  
    if (totalCount == 20) { 
     correct = true; 
     for (i = 1; i < refString.Length; i++) { 
      if(refString[i] == refString[i - 1]) { 
       // consecutive number 
       Console.WrinteLine("Error: same consecutive number "+refString[i]); 
       correct = false; 
       break; 
      } 
     } 
    }else{ 
     Console.WrinteLine("Error: not 20 elements"); 
    } 
} 
+0

感謝您的回答! – user3015999

1

試着這麼做:

case 2: 
    bool correctNumber = true; 
    bool repeats = false; 
    Console.WriteLine("\nPlease enter a 20 character reference string, each seperated by a single space"); 
    Console.WriteLine("A number should not occur twice in a row, ex: 1 5 4 4 6"); 
    Console.WriteLine("Each character must be an integer 1-9"); 
    Console.WriteLine("Example reference string: 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6"); 
    Console.WriteLine("\nEnter reference string: "); 
    string s = Console.ReadLine(); 
    refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); //split string and add numbers into array 
    correctNumber = refString.Length == 20; 

    if (correctNumber) 
    { 
     for (i = 1; i < 20; i++) 
     { 
      if(refString[i] == refString[i - 1]) 
      { 
       repeats = true; 
       break; 
      } 
     } 
    } 
    else 
    { 
     Console.WriteLine("Error: reference string is NOT 20 numbers"); 
    } 
    if (repeats) 
    { 
     Console.WriteLine("Error: reference string contains repeated numbers"); 
    } 

    break; 

,將檢查兩個失敗的情況和輸出相關消息。

+0

謝謝你的回覆。我能夠使它工作並完成大多數錯誤檢查。我沒有做的錯誤處理的唯一方法是防止不良輸入,例如用戶輸入字母以及用戶輸入兩位數字。你介意解釋如何做? isNaN? – user3015999

0

一個竅門,你可以做的是使用LINQ過濾掉所有的錯誤值,然後檢查計數未跌破20:

var listOfInts = stringTest.Split(' ').Select(n => Convert.ToInt32(n)).ToList(); 

// filter ints with adjacent values and out of range 
var count = 
    listOfInts.Where((i, j) => (j + 1 == listOfInts.Count || // adjacent values 
           i != listOfInts[j + 1]) 
           && i > 0 && i < 10) // out of range 
       .Count(); // total 

如果算上< 20,那麼你就知道什麼是無效的(或者超出範圍或者接近相同的值)。

相關問題