简体   繁体   English

C#-验证清单

[英]C# - Verify List

So I have this code to verify if the 4 colors (array "cores_dos_pontos_medios") appears 3 straight frames (from video feed). 因此,我有这段代码可以验证4种颜色(数组“ cores_dos_pontos_medios”)是否出现3个连续帧(来自视频供稿)。 If it does I can do: 如果可以,我可以做:

posicao = loc.Coordenadas(cores_dos_pontos_medios); //obtem as coordenadas atraves das cores

The problem is that it's not verifying correctly. 问题是它没有正确验证。 It still sends cores_dos_pontos_medios to loc even tho it doesnt appear 3 times straight. 它仍然发送cores_dos_pontos_medios来定位,即使它没有连续3次出现。

cores = cores_dos_pontos_medios[0] + " , " + cores_dos_pontos_medios[1] + " , " + cores_dos_pontos_medios[2] + " , " + cores_dos_pontos_medios[3];

int n_de_verificacoes_cores = 3;

if (lista_cores.Count >= n_de_verificacoes_cores)
{
    lista_cores.RemoveAt(0);
}
lista_cores.Add(cores);


if (lista_cores.Count >= n_de_verificacoes_cores && lista_cores.Any(s => s == lista_cores[0]))
{

    posicao = loc.Coordenadas(cores_dos_pontos_medios); //obtem as coordenadas atraves das cores

EDIT: now that I think about it maybe I need to remove everything on the list lista_cores? 编辑:现在,我考虑一下,也许我需要删除列表lista_cores上的所有内容?

The problem is that lista_cores.Any(s => s == lista_cores[0]) is always true because this instruction is checking if any of the element of lista_cores is equal to the first element of lista_cores. 问题在于lista_cores.Any(s => s == lista_cores[0])始终为true,因为此指令正在检查lista_cores的任何元素是否等于lista_cores的第一个元素。 If what you want to do is check if all elements are equal you have to use lista_cores.All(s => s == lista_cores[0]) 如果要检查所有元素是否相等,则必须使用lista_cores.All(s => s == lista_cores[0])

From your " I wanted to compare if all the string on that list have the same text " comment I suppose you want something like this, but I didnt understand most of your question.. 从您的“ 我想比较列表中的所有字符串是否都具有相同文本 ”的注释中,我想您需要这样的内容,但是我大部分都不理解。

bool containsInvalidData = false;
foreach(string valueToCheck in lista_cores)
{
    if(lista_cores.Any(p => valueToCheck != p)) containsInvalidData = true;
    break;
}

You can also do it in a single call, using List.ForEach() function 您还可以使用List.ForEach()函数在单个调用中完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM