读取一个字符串,将其分开,并将其放入数组C#中

时间:2013-11-21 04:56:40

标签: c# arrays split

我试图通过用户的输入读取字符串并将其存储在数组中。字符串必须是由空格分隔的单个数字。用户将输入正好20个数字,连续两次不能出现任何数字。

示例字符串:

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;
}

4 个答案:

答案 0 :(得分: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;

这将检查两个失败条件并输出相关消息。

答案 1 :(得分:0)

您可以使用正则表达式检查是否有两个连续的重复数字

if(!Regex.IsMatch(n,@".*(\d) \1.*"))
    s.Split(" ").Select(x=>int.Parse(x));

答案 2 :(得分: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");
    }
}

答案 3 :(得分: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

如果计数&lt; 20,然后你知道某些东西是无效的(超出范围或者在相同的值旁边)。