0

首先,我是 C# 新手,需要一些帮助。我是一个包含列表的类。我可以从应用程序设置列表中的项目,但不能从类(需要在哪里完成)。我还需要将活动转移到课堂上。这也适用于应用程序。对此的任何帮助将不胜感激。下面是我课堂上的代码:

    namespace CarRace
{
class Cars
{
    public string Name { get; set; }
    public int StartPOS { get; set; }
    public int Speed { get; set; }
    public int CurPOS { get; set; }
    public double Location { get; set; }
    public Cars(string Name, int StartPOS, int Speed, int CurPOS, long Location)
    {
        this.Name = Name;
        this.StartPOS = StartPOS;
        this.Speed = Speed;
        this.CurPOS = 0;
        this.Location = 0;
    }
    public static int CompareCurrentLocation(Cars c1, Cars c2)
    {
        return c2.Location.CompareTo(c1.Location);
    }
    }
}

我需要将此添加到课程中:

if (File.Exists("NewRace.xml"))
            {
                XDocument doc = XDocument.Load("NewRace.xml");
                var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0)
                {
                    Name = x.Attribute("Name").Value,
                    StartPOS = int.Parse(x.Attribute("StartPOS").Value),
                    Speed = int.Parse(x.Attribute("Speed").Value),
                    CurPOS = 0
                });
            }

和这个:

int p = 1;
        int prevPOS = 1;
        DateTime? PrevTime;
        double dist = 0;
        double PrevLoc = 0;
        if (i == 0)
        {
            return;
        }
        foreach (var car in RaceList)
        {
            dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
            car.Location = car.Location + dist;
        }
        Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
        RaceList.Sort(compLoc);
        PrevTime = DateTime.Now;
        foreach (var car in RaceList)
        {
            if (car.Location != PrevLoc)
            {
                car.CurPOS = p;
            }
            else
            {
                car.CurPOS = prevPOS;
            }
            prevPOS = car.CurPOS;
            PrevLoc = car.Location;
            p++;
        }

谢谢

4

2 回答 2

1

感谢您的所有回复。我尝试了 car.speed/3600 但只得到了 0 所以我做了很长的路。我确实让一切都按照我需要的方式工作。

那是因为您正在进行整数除法car.Speedis of type int),所以结果的小数部分被丢弃。由于汽车的速度低于 3600,因此这将始终导致零。car.Speed您可以通过转换为first来避免这种情况double- 这应该会产生您想要的结果:

dist = (double)car.Speed / 3600;
于 2012-07-08T03:23:02.917 回答
0

感谢您的所有回复。我尝试了 car.speed/3600 但只得到了 0 所以我做了很长的路。我确实让一切都按照我需要的方式工作。我是这方面的新手,非常感谢任何提示。下面是我更新的代码。

namespace CarRace
{
class Cars
{
    public string Name { get; set; }
    public int StartPOS { get; set; }
    public int CurPOS { get; set; }
    public int Speed { get; set; }
    public double Location { get; set; }
    public string Make { get; set; }
    private static List<Cars> CarList;
    private static string ProgPart;
    public Cars(string Name, int StartPOS, int CurPOS, int Speed, double Location, string Make)
    {
        this.Name = Name;
        this.StartPOS = StartPOS;
        this.CurPOS = CurPOS;
        this.Speed = Speed;
        this.Location = Location;
        this.Make = Make;
    }
    public static List<Cars> FillList()
    {
        return CarList;
    }
    public static void Main()
    {
        try
        {
            bool Prog = false;
            //Get Part to display
            while (!Prog)
            {
                Console.WriteLine("Select the part you would like to test.(1 or 2)");
                ProgPart = Console.ReadLine();
                if (ProgPart == "1" || ProgPart == "2")
                {
                    Prog = true;
                }
            }
            Console.WriteLine("----------------------------------------");
            //Read XML File and set the CarList
            if (File.Exists("NewRace.xml"))
            {
                XDocument doc = XDocument.Load("NewRace.xml");
                var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0, "")
                {
                    Name = x.Attribute("Name").Value,
                    StartPOS = int.Parse(x.Element("StartPOS").Value),
                    CurPOS = 0,
                    Speed = int.Parse(x.Element("Speed").Value),
                    Location = double.Parse(x.Element("Location").Value),
                    Make = x.Attribute("Make").Value
                });
                CarList = new List<Cars>();
                foreach (var car1 in nCars)
                {
                    if (ProgPart == "1")
                    {
                        CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, car1.Speed, car1.Location, car1.Make));
                    }
                    else
                    {
                        CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, 0, car1.Location, car1.Make));
                    }
                }
            }
            else
            {
                Console.WriteLine("File Not Found!");
                Console.ReadKey();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    public static int CompareCurrentLocation(Cars c1, Cars c2)
    {
        return c2.Location.CompareTo(c1.Location);
    }
    public static Boolean UpdateLeaderBoard(long i)
    {
        try
        {
            int p = 1;
            int prevPOS = 1;
            DateTime? PrevTime;
            double dist = 0;
            double prevLocation = 0;
            if (i == 0)
            {
                return false;
            }
            foreach (var car in CarList)
            {
                if (ProgPart == "2")
                {
                    switch (car.Make)
                    {
                        case "300ZX":
                            //Slow continuous gain of speed by percentage after 60 with top speed of 320.
                            if (i <= 15)
                            {
                                car.Speed = car.Speed + (60 / 10);
                            }
                            else if (car.Speed < 320)
                            {
                                double percent = (double)(car.Speed * .1);
                                if ((Convert.ToInt32(car.Speed + percent)) > 320)
                                {
                                    car.Speed = 320;
                                }
                                else
                                {
                                    car.Speed = Convert.ToInt32(car.Speed + (percent));
                                }
                            }
                            break;
                        case "Camero":
                            //0-60 4 seconds 60-220 20 seconds Top 220
                            if (i <= 4)
                            {
                                car.Speed = car.Speed + (60 / 4);
                            }
                            else if (i <= 20)
                            {
                                car.Speed = car.Speed + 10;
                            }
                            break;
                        case "Mustang":
                            //0-top(210) 25 seconds
                            if (car.Speed <= 200)
                            {
                                car.Speed = car.Speed + (200 / 20);
                            }
                            break;
                        case "VW Bug":
                            //Constant Speed
                            car.Speed = 165;
                            break;
                        default:
                            Console.WriteLine("Make not found");
                            break;
                    }
                }
                //Set Cars Current Location
                dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
                car.Location = car.Location + dist;
            }
            //Sort list by location
            Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
            CarList.Sort(compLoc);
            PrevTime = DateTime.Now;
            //Set the Current Position
            foreach (var car in CarList)
            {
                if (car.Location != prevLocation)
                {
                    car.CurPOS = p;
                }
                else
                {
                    car.CurPOS = prevPOS;
                }
                prevPOS = car.CurPOS;
                prevLocation = car.Location;
                p++;
            }
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
}

}

于 2012-07-08T02:59:33.520 回答