写随机点名的程序,抽取三个人名,并且进行控制台输出。 1班有八个人,"张三","李四","王五","赵六","孙七","周八","吴九","郑十" 2班有八个人,"张三2","李四2","王五2","赵六2","孙七2","周八2","吴九2","郑十2",用c#
时间: 2023-12-08 16:38:37 浏览: 147
以下是用C#编写的随机点名程序,抽取三个人名并进行控制台输出:
```csharp
using System;
using System.Collections.Generic;
namespace RandomName
{
class Program
{
static void Main(string[] args)
{
// 初始化班级名单
List<string> class1 = new List<string>() { "张三", "李四", "王五", "赵六", "孙七", "周八", "吴九", "郑十" };
List<string> class2 = new List<string>() { "张三2", "李四2", "王五2", "赵六2", "孙七2", "周八2", "吴九2", "郑十2" };
// 从班级名单中随机抽取三个人
List<string> selectedNames = new List<string>();
Random random = new Random();
int class1Count = class1.Count;
int class2Count = class2.Count;
int selectedCount = 0;
while (selectedCount < 3)
{
int classIndex = random.Next(2);
List<string> selectedClass = classIndex == 0 ? class1 : class2;
int nameIndex = random.Next(selectedClass.Count);
string selectedName = selectedClass[nameIndex];
if (!selectedNames.Contains(selectedName))
{
selectedNames.Add(selectedName);
selectedCount++;
}
}
// 输出抽取的三个人名
Console.WriteLine("抽取的三个人名为:");
foreach (string name in selectedNames)
{
Console.WriteLine(name);
}
}
}
}
```
阅读全文