写随机点名的程序,抽取三个人名,并且进行控制台输出。 1班有八个人,"张三","李四","王五","赵六","孙七","周八","吴九","郑十" 2班有八个人,"张三2","李四2","王五2","赵六2","孙七2","周八2","吴九2","郑十2",用c#
时间: 2023-12-08 18:38:37 浏览: 140
六班幸运抽奖点名_抽奖_一个随机点名程序_
5星 · 资源好评率100%
以下是用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);
}
}
}
}
```
阅读全文