每次点名从1到number
被点过的数字不会出现,直到全部数字被点一遍
程序打开读取上次点名信息,关闭存储点名信息
class Program
{
static int number = 9;
static List<int> arr = new List<int>();
static void Main(string[] args)
{
rollCall();
}
static void rollCall()
{
string[] arrStrs = File.ReadAllLines("arrText.txt");
if(arrStrs.Length > 0)
{
foreach (var item in arrStrs)
{
arr.Add(int.Parse(item));
}
}
while (Console.ReadLine() != "end")
{
Console.WriteLine(GetRandomNumber(arr));
}
string[] tempArr = new string[arr.Count];
for (int i = 0; i < arr.Count; i++)
{
tempArr[i] = arr[i].ToString();
}
File.WriteAllLines("arrText.txt", tempArr);
}
static int GetRandomNumber(List<int> arr)
{
if(arr.Count <= 0)
{
int arrLength = number;
for(int i = 0; i < arrLength; i++)
{
arr.Add(i + 1);
}
}
Random random = new Random();
int randomIndex = random.Next(0, arr.Count);
int temp = arr[randomIndex];
arr.RemoveAt(randomIndex);
return temp;
}
}
网友评论