写随机点名的程序,抽取三个人名,并且进行控制台输出。 1班有八个人,"张三","李四","王五","赵六","孙七","周八","吴九","郑十" 2班有八个人,"张三2","李四2","王五2","赵六2","孙七2","周八2","吴九2","郑十2"
时间: 2023-12-08 08:38:41 浏览: 66
可以使用Random类来实现随机点名程序,具体实现如下:
```csharp
using System;
namespace RandomName
{
class Program
{
static void Main(string[] args)
{
string[] class1 = { "张三", "李四", "王五", "赵六", "孙七", "周八", "吴九", "郑十" };
string[] class2 = { "张三2", "李四2", "王五2", "赵六2", "孙七2", "周八2", "吴九2", "郑十2" };
Random random = new Random();
int index1 = random.Next(0, class1.Length);
int index2 = random.Next(0, class2.Length);
int index3 = random.Next(0, class1.Length);
Console.WriteLine("抽取的三个人名为:");
Console.WriteLine(class1[index1]);
Console.WriteLine(class2[index2]);
Console.WriteLine(class1[index3]);
Console.ReadKey();
}
}
}
```
以上代码中,我们首先定义了两个班级的学生名单,然后使用Random类生成三个随机数,分别对应三个被抽取的学生的下标。最后将这三个学生名字输出到控制台上。
阅读全文