How to add elements to an array based on a condition?

时间:2016-02-03 04:10:41

标签: c# arrays

I'm working on this simple C# program adding elements to an array. I allow the user to enter 5 numbers, and if the user enters an INVALID valid I have a message for that. My issue is that whether the users enters an invalid number or not I still want to add 5 numbers to my array.

My code works, but let's say the user enters 3 numbers and then 2 words and I end up having ONLY 3 numbers, but I want the 5 numbers no matter what. What am I doing wrong?

Here's my code:

        int[] numbers = new int[5];

        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Enter a number: ");
            string c = Console.ReadLine();
            int value;
            if (int.TryParse(c, out value))
            {
                numbers[i] = value;
            }
            else
            {
                Console.WriteLine("You did not enter a number\n");
            }
        }

        for (int i = 0; i < numbers.Length; i++ )
        {
            Console.Write(numbers[i] + " ");
        }

4 个答案:

答案 0 :(得分:3)

You can reduced increment count by 1, when user inputs wrong/no number.

Also note, you are code currently reading input only for 4(not 5 as question description says.) numbers.

int[] numbers = new int[4];

for (int i = 0; i < 4; i++)
{
    Console.WriteLine("Enter a number: ");
    string c = Console.ReadLine();
    int value;
    if (int.TryParse(c, out value))
    {
        numbers[i] = value;
    }
    else
    {
        i--;
        Console.WriteLine("You did not enter a number\n");
    }
}

for (int i = 0; i < numbers.Length; i++ )
{
    Console.Write(numbers[i] + " ");
}

答案 1 :(得分:3)

try using do-while

 int[] numbers = new int[4];


    int i = 0;
    do
    {
       Console.WriteLine("Enter a number: ");
         string c = Console.ReadLine();
            int value;
            if (int.TryParse(c, out value))
            {
                numbers[i] = value;
                i++;
            }
         else
            {
                Console.WriteLine("You did not enter a number\n");
            }

    } while (i < 5);

    Console.WriteLine("\nYour entered numbers are\n");
         for (int j = 0; j < numbers.Length; j++ )
        {
            Console.Write(numbers[j] + " ");
        }

答案 2 :(得分:0)

You can reduce the code using while loop. Also its better to change the last for loop to foreach

int[] numbers = new int[5];
int i = 0;
while (i < 5)
{
    Console.WriteLine("Enter a number: ");
    string c = Console.ReadLine();
    int value;
    if (!int.TryParse(c, out value)) continue;
    numbers[i] = value;
    i++;
}

foreach (int t in numbers)
    Console.Write(t + " ");

答案 3 :(得分:0)

You could use while loop here. See the below code

     int[] numbers = new int[5];
     int i = 0;
     while (i < 5) {
        Console.WriteLine ("Enter a number: ");
        string c = Console.ReadLine ();
        int value;
        if (int.TryParse (c, out value)) {
           numbers[i] = value;
           i++;
        } else {
           Console.WriteLine ("You did not enter a number\n");
        }
     }

     for (i = 0; i < numbers.Length; i++) {
        Console.Write (numbers[i] + " ");
     }