-4

我需要帮助编写一个代码,让用户在不重复相同数字的情况下输入 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);
            }              
        }
    }
}
4

1 回答 1

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);
    }
}
于 2013-01-31T22:18:40.423 回答