C#用户输入随机数

时间:2013-01-31 22:11:22

标签: c# loops if-statement random input

我需要帮助编写一个代码,作为用户输入50到150之间的10个数字而不重复相同的数字。这将是一个循环程序。这是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IP3_Program
{
    class Program
    {
        static void Main()
        {
            int total = 0;
            string inValue;
            int [] number = new int[10];
            for (int i = 0; i < number.Length; i++)
            {
                Console.Write("Enter number{0}: ", i + 1);
                inValue = Console.ReadLine();
                number[i] = Convert.ToInt32(inValue);
            }              
        }
    }
}

1 个答案:

答案 0 :(得分:1)

var numbers = new List<int>();
while (numbers.Count != 10)
{
    // get number from user

    if (numbers.Contains(newNumber) || newNumber < 50 || newNumber > 150)
    {
        // reject the number
        // you'd probably want to display a message here
        // alerting the user another input in needed
    }
    else
    {
        numbers.Add(newNumber);
    }
}