Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在同一列火车上找到方向?_C#_Next_Directions - Fatal编程技术网

C# 如何在同一列火车上找到方向?

C# 如何在同一列火车上找到方向?,c#,next,directions,C#,Next,Directions,你能帮我一步一步的逻辑吗?我需要在同一条火车线上找到一个方向。已经有了下一个和上一个功能的通用列车线 public IStation Next(IStation s) { if (!_stations.Contains(s)) { throw new ArgumentException(); } var index = _stations.IndexOf(s); var isLast = index == _stations.Count -

你能帮我一步一步的逻辑吗?我需要在同一条火车线上找到一个方向。已经有了下一个和上一个功能的通用列车线

public IStation Next(IStation s)
{
    if (!_stations.Contains(s))
    {
        throw new ArgumentException();
    }
    var index = _stations.IndexOf(s);
    var isLast = index == _stations.Count -1;
    if (isLast)
    {
        return null;
    }
    return _stations[index + 1];
}

public IStation Previous(IStation s)
{
    if (!_stations.Contains(s))
    {
        throw new ArgumentException();
    }
    var index = _stations.IndexOf(s);
    var isFirst = index == 0;
    if (isFirst)
    {
        return null;
    }
    return _stations[index - 1];
}
以及我寻找方向的功能

public string GetLineDirectiom(Station from, Station to, Line commonLine)
{
    bool fromNextTo = true;


    //to.Lines.Count();
    //to.ToString();
    var Final = commonLine.Next(from);
    while (Final != null)
    {

    }

    if (fromNextTo)
        return "next";
    else return "previous";
}

不清楚您想做什么以及为什么返回字符串“next”和“prev”作为方向,但通常通过两个电台获取方向:

    public int GetStationIndex(IStation s)
    {
        var index = _stations.IndexOf(s);
        if (index == -1)
        {
           throw new ArgumentException();
        }

        return index ;
    }


    public string GetLineDirection(Station from, Station to, Line commonLine)
    {
       var direction = commonLine.GetStationIndex(from)<commonLine.GetStationIndex(to)?"next" : "previous" 
       return direction;
    }
public int GetStationIndex(IStation s)
{
var指数=_站。指数f(s);
如果(索引==-1)
{
抛出新ArgumentException();
}
收益指数;
}
公共字符串GetLineDirection(车站起点、车站终点、线路公共线)
{

var direction=commonLine.GetStationIndex(from)看起来您正试图“沿着
commonLine
访问站点”,从
from
站点开始

您启动的循环是该目的的有效起点;您需要一个变量来存储您当前访问的站点。可能当前变量名
Final
让您有点困惑,因为它不是线路的“Final”站点,而只是您当前访问的站点

因此,让我们将变量命名为
currentStation
。然后,您希望转到下一个站点,直到找到
(从而知道方向),或者直到到达行的末尾:

var currentStation = from;
while (currentStation != null)
{
    if (currentStation == to)
    {
        return "next";
    }
    currentStation = commonLine.Next(currentStation);
}
现在,这将检查
是否为“前方”。如果不是,您可以继续检查是否可以在另一个方向找到它,再次从
开始:

currentStation = from;
while (currentStation != null)
{
    if (currentStation == to)
    {
        return "previous";
    }
    currentStation = commonLine.Previous(currentStation);
}
如果此循环也没有找到
,显然
不在线。请根据您的偏好处理此情况

一些评论:

  • 将方向指示为“下一个”或“上一个”可能有点误导。如果它确实是行的方向,请考虑“前进”和“后退”之类的内容,因为“下一个”和“上一个”确实意味着列表中直接的下一个/上一个元素
  • 虽然上述方法有效,但我注意到您的
    对象在索引列表中已经有了站点。因此,实现您的目标的一种更简单的方法可能是确定
    公共行
    上的站点的索引,并比较哪一个大于另一个

什么是
行公共线
?在检查"stations.Contains"和"stations.IndexOf"时,你要做双重工作。如果找不到station,IndexOf将返回-1。请修复它:)@PavelAnikhouski:显然,是一个
类的实例,它包含来自fi的
下一个
上一个
方法rst代码块。