C# 随机生成姓名、性别、学历、职业及收入、公司人数
时间: 2023-09-16 22:12:14 浏览: 188
以下是一个简单的 C# 代码示例,可以随机生成姓名、性别、学历、职业、收入和公司人数:
```csharp
using System;
class Program
{
static Random rand = new Random();
static void Main(string[] args)
{
// 生成随机的姓名
string[] firstNames = { "张", "李", "王", "赵", "陈", "刘", "周", "吴", "黄", "郑" };
string[] lastNames = { "三", "四", "五", "六", "七", "八", "九", "十", "强", "华" };
string firstName = firstNames[rand.Next(0, firstNames.Length)];
string lastName = lastNames[rand.Next(0, lastNames.Length)];
string name = firstName + lastName;
// 生成随机的性别
string[] genders = { "男", "女" };
string gender = genders[rand.Next(0, genders.Length)];
// 生成随机的学历
string[] educations = { "小学", "初中", "高中", "大学", "研究生", "博士" };
string education = educations[rand.Next(0, educations.Length)];
// 生成随机的职业和收入
string[] professions = { "程序员", "销售员", "教师", "医生", "律师", "工人" };
string profession = professions[rand.Next(0, professions.Length)];
int income = rand.Next(2000, 20000);
// 生成随机的公司人数
int companySize = rand.Next(10, 1000);
// 输出结果
Console.WriteLine("姓名:{0}", name);
Console.WriteLine("性别:{0}", gender);
Console.WriteLine("学历:{0}", education);
Console.WriteLine("职业:{0}", profession);
Console.WriteLine("收入:{0}元", income);
Console.WriteLine("公司人数:{0}", companySize);
}
}
```
该代码使用了 `Random` 类来生成随机数,然后通过数组来随机选择不同的选项,最后输出生成的结果。您可以根据需要调整数组中的选项和生成的范围。
阅读全文