输入数字,第一个决定将有多少

时间:2014-04-21 12:17:22

标签: c# console

我需要从控制台输入中为数组添加一些数字,其中第一个数字决定将有多少数字(该数字* 2)。这些数字最多有2位数。 到目前为止,我的代码仍然无法正常工作。

        int quantity = Convert.ToInt32(Console.Read());          

        int[] arr = new int[100];               
        int count = 0;

        while (count < quantity*2)             
        {
            string line = Console.ReadLine();
            line = line + ' ';

            for (int i = 0; i < line.Length; i++)
            {
                char c = line[i];
                if ((c>='0')&&(c<='9'))
                {
                    char d = line[i + 1];
                    if ((d >= '0') && (d <= '9'))
                    {
                        arr[count] = c * 10 + d;
                        i++;
                    }
                    else 
                    {
                        arr[count] = c;
                    }
                    count++;

                }
            }

2 个答案:

答案 0 :(得分:2)

有一些错误导致程序无法正常运行。

// Do not use Console.Read, but ReadLine to wait the end of input from your user
string userInput Console.ReadLine();          

// Convert to a number using Int32.TryParse to check if your user really types a number
// and not something that will raise an exception if it is not a number
int quantity;
if(!Int32.TryParse(userInput, out quantity))
{
    Console.WriteLine("An integer number is required!");
    return;
}

// Dimension you array size large enough to contain the numbers required in the
// following loop (I am not sure if now you require to double the size of the array)
int[] arr = new int[quantity*2];               
int count = 0;

while (count < quantity*2)             
{
    string line = Console.ReadLine();
    int inputNumber;

    // Again to convert to a number, use Int32.TryParse 
    if(Int32.TryParse(line, inputNumber))
    {
        arr[count] = inputNumber;
        count++;
    }
    else
    {
        Console.WriteLine("An integer number is required!");
    }
 }

使用Int32.TryParse可以更好地处理用户输入。无需使用搜索无效输入的各个字符。 (顺便说一句,您的实际方法将代码限制为不超过两位数的数字)

但是,在这样的上下文中,当您不知道数组的确切大小时,建议使用的数据结构为List<int>

// Create a List of integers, the size is not needed
List<int> arr = new List<int>();
int count = 0;

// Not sure if now you require to double the size of the array
while (count < quantity)             
{
    string line = Console.ReadLine();
    int inputNumber;

    if(Int32.TryParse(line, inputNumber))
    {
        // Add the input to the list....
        arr.Add(inputNumber);
        count++;
    }
    else
    {
        Console.WriteLine("An integer number is required!");
    }
 }

您可以轻松使用List,因为它是一个数组

 for(int x = 0; x < arr.Count; x++)
     Console.WriteLine(arr[x].ToString());

编辑在下面发表评论。如果该行可以包含多个数字,那么您需要对用户输入进行拆分,但这不会影响TryParse检查,您只需要一个额外的循环并控制插入的元素的最大数量

int[] arr = new int[quantity];               
int count = 0;
while (count < quantity)             
{
    string line = Console.ReadLine();
    int inputNumber;

    // Split on space and tabs and remove the resulting 
    // empty elements if there are two space/tabs consecutive.
    string[] parts = line.Split(new char[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
    foreach(string aNumber in parts)
    {
         if(Int32.TryParse(line, inputNumber))
         {
             arr[count] = inputNumber;
             count++;
         }
         else
         {
              Console.WriteLine(aNumber.ToString() + " is not an integer number!");
         }
         if(count >= arr.Length)
             break;
    }
}

答案 1 :(得分:0)

我认为这有效:

Console.Write("Enter the number to collect (*2):");
int initialQuantity = Convert.ToInt32(Console.ReadLine());
int calculatedQuantity = initialQuantity * 2;
int[] arr = new int[calculatedQuantity];
int count = 0;

while (count < calculatedQuantity)
{
    Console.Write("Enter the number:");
    string line = Console.ReadLine();
    int inputNumber;
    if (int.TryParse(line, out inputNumber) && line.Length <= 2)//is valid
        arr[count++] = inputNumber;
}