C#-布尔和if语句
我目前正在用Visual Studio上的GoogleAPI Vision编写分析图像的代码。 但是我在循环过程中遇到了一个问题。 分析返回注释列表(汽车、车辆、陆地车辆等),我想用“如果”对其进行过滤,因此我写了以下内容:C#-布尔和if语句,c#,C#,我目前正在用Visual Studio上的GoogleAPI Vision编写分析图像的代码。 但是我在循环过程中遇到了一个问题。 分析返回注释列表(汽车、车辆、陆地车辆等),我想用“如果”对其进行过滤,因此我写了以下内容: var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\1.jpg"); var client = ImageAnnotatorClient.Create(); var response
var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\1.jpg");
var client = ImageAnnotatorClient.Create();
var response = client.DetectLabels(image);
CropHintsAnnotation confidence = client.DetectCropHints(image);
bool empty = false;
foreach (var annotation in response)
{
textBox1.Text += annotation.Description + "\r\n";
textBox1.Text += "Score : " + annotation.Score + "\r\n";
if (annotation.Description.Equals("vehicle"))
{
empty = false;
}
else
{
empty = true;
}
}
textBox1.Text += "\r\nEmpty ? " + empty + "\r\n\r\n";
所以,如果我写得好,它应该说“空?假”,因为分析只返回“vehicle”一次。
我还试图替换:
annotation.Description.Equals("vehicle")
借
但没有办法,它仍然说“空?真”,因为它不应该这样做
有什么想法吗
提前感谢您阅读本文并提供帮助 不完全确定您在这里尝试执行的操作,但假设
响应
包含多个项,空
变量将仅表示最后一个项的值
原因是,对于循环的每次迭代,当它到达if
语句时,它将输入它或else
,并且肯定会输入这两个语句中的一个,因此对于每次迭代,empty
的值将被分配并覆盖上一个值
至于代码本身,这样写更整洁:
empty = !annotation.Description.Equals("vehicle");
您应该更改的是将分配的行移动到循环中:
foreach(/*...*/)
{
/*...*/
empty = !annotation.Description.Equals("vehicle");
textBox1.Text += "\r\nEmpty ? " + empty + "\r\n\r\n";
}
不完全确定您在这里尝试执行的操作,但假设
响应
包含多个项,空
变量将仅表示最后一个项的值
原因是,对于循环的每次迭代,当它到达if
语句时,它将输入它或else
,并且肯定会输入这两个语句中的一个,因此对于每次迭代,empty
的值将被分配并覆盖上一个值
至于代码本身,这样写更整洁:
empty = !annotation.Description.Equals("vehicle");
您应该更改的是将分配的行移动到循环中:
foreach(/*...*/)
{
/*...*/
empty = !annotation.Description.Equals("vehicle");
textBox1.Text += "\r\nEmpty ? " + empty + "\r\n\r\n";
}
几乎没有可能:
foreach (var annotation in response)
{
// not sure if you want this to
textBox1.Text += annotation.Description + "\r\n";
textBox1.Text += "Score : " + annotation.Score + "\r\n";
if (annotation.Description.Equals("vehicle",
StringComparison.InvariantCultureIgnoreCase))
{
empty = false;
textBox1.Text += "\r\nEmpty ? " + empty + "\r\n\r\n";
//possibly also break if you've achieved what you want.
break;
}
}
foreach (var annotation in response)
{
// not sure if you want this to
textBox1.Text += annotation.Description + "\r\n";
textBox1.Text += "Score : " + annotation.Score + "\r\n";
if (annotation.Description.Equals("vehicle",
StringComparison.InvariantCultureIgnoreCase))
{
empty = false;
textBox1.Text += "\r\nEmpty ? " + empty + "\r\n\r\n";
//possibly also break if you've achieved what you want.
break;
}
}
您是否考虑过返回字符串的情况?试着忽略这个案例:
annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)
请注意:Equals
函数返回一个Boolean
,因此您可以删除整个if
语句,并将代码简化为:
empty = !annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)
您是否考虑过返回字符串的情况?试着忽略这个案例:
annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)
请注意:Equals
函数返回一个Boolean
,因此您可以删除整个if
语句,并将代码简化为:
empty = !annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)
你介意花几分钟时间提高你的英语水平吗?我无法理解您的问题。只有当最后一个批注等于“vehicle”时,才会返回
true
,因为您会覆盖每个批注的empty
值。你应该使用empty |=annotation.Description.equalast(“车辆”)
你介意花几分钟时间提高你的英语水平吗?我无法理解您的问题。只有当最后一个批注等于“vehicle”时,才会返回true
,因为您会覆盖每个批注的empty
值。您应该使用empty |=annotation.Description.Equast(“车辆”)
我不明白的是,我的空值只是最后一行。我应该早点看到这一点(我不明白的是,我的空值只是最后一行。我应该早点看到这一点(