foreach(string strs in drivers)
{
if(string.Equals(strs,checkingdriver))
bo=true;
else
bo=false;
}输入:小胡,虽然他是是第一辆车驾驶员,但是foreach最后为比较小胡和小磊改为 public bool DriverCheck(string checkingdriver)
{
bool bo=false;
foreach(string strs in drivers)
   if(string.Equals(strs,checkingdriver))
              bo=true;

return bo;
}

解决方案 »

  1.   

    else if(firstcar.DriverCheck(driverName)) 应为secondcar
    正确代码如下
    using System;class SportCar
    {
    private string [] drivers;

    public void SetLegalDrivers(params string [] newDrivers)
    {
    drivers=new string [newDrivers.Length];
    for(int i=0;i<newDrivers.Length;i++)
    {
    drivers[i]=newDrivers[i];
    }
    } public bool DriverCheck(string checkingdriver)
    {
    bool bo=false;
    foreach(string strs in drivers)
       if(string.Equals(strs,checkingdriver))
                  bo=true;

    return bo; }
    } class Tester
    {
    public static void Main()
    {
    SportCar firstcar=new SportCar();
    SportCar secondcar=new SportCar();
    string driverName=""; firstcar.SetLegalDrivers("a","b");
    secondcar.SetLegalDrivers("c","d"); Console.WriteLine("请输入你的名字:");
    driverName=Console.ReadLine();
    if(firstcar.DriverCheck(driverName))
    Console.WriteLine("你是第一辆车驾驶员");
    else if(secondcar.DriverCheck(driverName))
    Console.WriteLine("你是第二辆车的驾驶员");
    else
    Console.WriteLine("你不是这两辆车的驾驶员"); }
    }