新手提问(6)!
某部门某天要在部门中随机抽取2名员工 参加公司的才艺比赛,性别不限,会唱歌,跳舞,魔术表演者均可,年龄20-35之间,但若是男生,身高为 1.75以上,女生身高为 1.60以上,若选不到2位,视为自动弃权,请为该部门写一个小程序实现以上功能
(定义员工结构:工号,部门,姓名,年龄,特长,身高)
-----------------------------以上是题目————————————————————————————————
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication22
{
class Program
{
struct BM
{
public int id;
public string name;
public string dept;
public string features;
public double height;
public int age;
public string sex;
}
public static void man(int x)
{
int n = 0;
BM[] A = new BM[x];
ArrayList B = new ArrayList();
Random rand1 = new Random();//定义随机选取!
for (int i = 0; i < A.Length; i++)
{
Console.WriteLine("******************请输入员工信息*******************:");
Console.WriteLine("请输入第{0}员工编号:", i);
A[i].id = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第{0}员工姓名:", i);
A[i].name = Console.ReadLine();
Console.WriteLine("请输入第{0}员工身高:", i);
A[i].height = double.Parse(Console.ReadLine());
Console.WriteLine("请输入第{0}员工年龄:", i);
A[i].age = int.Parse(Console.ReadLine());
if (A[i].age < 0 && A[i].age > 70)
throw new Exception();
Console.WriteLine("请输入第{0}员工性别:", i);
A[i].sex = Console.ReadLine();
if (A[i].sex != "NA" && A[i].sex != "NV")
throw new Exception();
Console.WriteLine("请输入第{0}员工所在部门:", i);
A[i].dept = Console.ReadLine();
Console.WriteLine("请输入第{0}员工特长:", i);
A[i].features = Console.ReadLine();
}
for (int i = 0; i < A.Length; i++)
{
if (A[i].sex == "NA" && A[i].age <= 35 && A[i].age >= 20 && A[i].height > 1.75 || A[i].sex == "NV" && A[i].age <= 35 && A[i].age >= 20 && A[i].height > 1.60)
{
n++;
B.Add(A[i].name);//将符合条件的人的姓名 装入动态数组,这里是否装入正确!
}
}
if (n >= 2)
{
int a = rand1.Next();// 1 问题主要错误区!
object i =(ArrayList)B[a];// 2 问题主要错误区!
int b = rand1.Next();// 3 问题主要错误区!
object j=(ArrayList)B[b];// 4 问题主要错误区!
Console.WriteLine(i+""+j);// 将前面4句话在随机数组中选取的动态数组成员输出,
}
else
{
Console.WriteLine("由于该部门人员不足,视为自动弃权!");
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入员工人数");
int x = int.Parse(Console.ReadLine());
man(x);