Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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从列表中返回索引范围之外的值#_C#_List_Sorting - Fatal编程技术网

C# 使用C从列表中返回索引范围之外的值#

C# 使用C从列表中返回索引范围之外的值#,c#,list,sorting,C#,List,Sorting,我有一张单子。如果您输入的值在该列表中,则返回结果。即使值超出结果的范围-由于mod%,结果也将返回很难用文字来解释我的问题,所以让我们看看代码: List<int> list1 = new List<int>(){ 0, 1, 2, 3, 4, 5, 6 }; int value = 3; // what's this number to us? string result = ""; int starti

我有一张单子。如果您输入的值在该列表中,则返回结果。即使值超出结果的范围-由于mod%,结果也将返回很难用文字来解释我的问题,所以让我们看看代码

        List<int> list1 = new List<int>(){ 0, 1, 2, 3, 4, 5, 6 };
        int value = 3; // what's this number to us?
        string result = "";
        int starting_number = 3;

        if (value == list1[(list1.IndexOf(starting_number) + 2) % list1.Count()])
        { result = "yeah"; }
        else if (value == list1[(list1.IndexOf(starting_number) + 1) % list1.Count()])
        { result = "cool"; }
        else if (value == list1[(list1.IndexOf(starting_number) + 0) % list1.Count()])
        { result = "one"; }
        else if (value == list1[(list1.IndexOf(starting_number) - 1) % list1.Count()])
        { result = "noo"; }
        else {result = "oops cant find it"; }
list1=newlist(){0,1,2,3,4,5,6};
int值=3;//这个号码对我们来说是什么?
字符串结果=”;
int起始值=3;
if(value==list1[(list1.IndexOf(起始编号)+2)%list1.Count())
{result=“yes”;}
else if(value==list1[(list1.IndexOf(起始编号)+1)%list1.Count())
{result=“cool”;}
else if(value==list1[(list1.IndexOf(起始编号)+0)%list1.Count())
{result=“one”;}
else if(value==list1[(list1.IndexOf(起始编号)-1)%list1.Count())
{result=“noo”;}
否则{result=“oops找不到”;}
  • 起始号码。这个数字是常数。所有测量均与该数字有关
  • 值-我们输入的值。我们需要得到这个值的结果
  • 结果-只是一个字符串
这就是它的工作原理:

起始编号=3,值=2=>result=“noo”(因为2=IndexOf(起始编号)-1)

开始_number=6,value=0=>result=“cool”(6+1=0)

将出现启动\u number=0,value=6=>错误。(0-1=6)


如何改进代码以消除此错误?基本上,我可以获取的值是“开始编号”之后的值,但我无法获取“开始编号”之前的值。

  • (6+1=0)的情况下:列表看起来像6,0,1,2,3

  • 如果是(0-1=6):列表应为…4,5,6,0

如果你有任何问题,请提问。。用文字来解释这个问题确实很难,但我想通过例子可以更清楚地说明这一点

如何获取低于起始值的值?

请尝试使用此代码

list1[Math.Sign((list1.IndexOf(starting_number) - 1))*(list1.IndexOf(starting_number) - 1) % list1.Count()]
请尝试使用此代码

list1[Math.Sign((list1.IndexOf(starting_number) - 1))*(list1.IndexOf(starting_number) - 1) % list1.Count()]

如果我理解正确,您希望将所有整数映射到范围0到Count-1,并且您当前的公式是
x%Count
,它仅在
x>0
时有效。然后,如果您希望它适用于
x
的所有值,请尝试使用此
(x%count+count)%count
如果我理解正确,您希望将所有整数映射到范围0到count-1,并且您当前的公式是
x%count
,它仅在
x>0
时有效。然后,如果您希望它适用于
x
的所有值,请尝试这一个
(x%count+count)%count

您实际上还没有问任何问题……有什么想法可以改进代码以消除此错误吗?基本上我能抓取的值是在“开始编号”之后,但我不能在“开始编号”之前抓取值。这让我们想到:如何抓取低于“开始编号”的值?实际上你还没有问过一个问题……有什么想法如何改进代码以消除此错误?基本上,我能抓取的值是在“开始”之前的“开始”之后,但我不能抓取它们。这让我们想到:你如何抓取低于开始数的值?@Alex它也不能工作,因为Likurgs one在开始索引@Alex时给你的数字与我的不同,它也不能工作,因为Likurgs one在开始索引时给你的数字和我的不同