简体   繁体   English

C#, ”?” 和“:”运算符

[英]C#, "?" and ":" operators

I not found any solution for my problem so i ask, how ?我没有为我的问题找到任何解决方案,所以我问,如何? and : operators works when i have multiple statemants?:当我有多个声明时,操作符有效?

What i want to do, i have pixel on the middle pixel[pos] and pixels around, its look like:我想要做什么,我在中间pixel[pos]和周围像素上有像素,它看起来像:

0 0 0
0 x 0
0 0 0

x is a center pixel. x是中心像素。

Im checking, if i have any white ( zero ) pixel around it.我正在检查,如果我周围有任何白色( zero )像素。 If is anyone, i marked pixel as two .如果有人,我将像素标记为two If not, so pattern looks like:如果没有,那么模式看起来像:

1 1 1
1 x 1
1 1 1

1 is an black pixel, i set it to one . 1是一个黑色像素,我将其设置为one

Now, the code:现在,代码:

if(pixels[positionOfPixel] == one && x > 0 && x < width 
                                  && y > 0 && y < height)
{
    pixels[positionOfPixel] = pixels[positionOfPixel - 1]          == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + 1]          == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset]     == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + offset]     == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + offset - 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset - 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two : zero;
}

My question is, why every one pixel is marked as two ?我的问题是,为什么每one像素标记为two Why it didnt recognize pixel, where every pixel arond is one (like in second pattern)?为什么它不识别像素,其中每个像素都是one (如第二个模式)?

Thanks for any advices!感谢您的任何建议!

I'm not a C# specialist, but there is common rule how ?我不是 C# 专家,但有一个共同的规则如何? : operator can be used. : 可以使用运算符。

x = (boolean condition) ? reult_if_true : result_if_false;

Eg例如

drink = isThisPersonAGirl ? wine : beer;

If you want to use many conditions with ?如果要使用许多条件与 ? : operator, you should do it that way: : 运营商,你应该这样做:

x = (boolean condition 1) ? result_if_true : (boolean condition 2) ? result_if_bool_2_is_true : result_if false;

Eg例如

drink = isThisPersonAChild ? lemonade : isThisPersonAGitl ? wine : beer

In your code snippet, it's hard to understand what's going on because you use = operator too often.在您的代码片段中,很难理解发生了什么,因为您经常使用 = 运算符。 In most languages, you could initalize several variables like this:在大多数语言中,您可以像这样初始化几个变量:

a = b = c = 0, so a, b, c will be = 0; a = b = c = 0,所以 a、b、c 将 = 0;

So I think your mistake is using = operator too often so maybe only this condition does matter, while others are simply skipped:所以我认为你的错误是过于频繁地使用 = 运算符,所以也许只有这个条件才重要,而其他条件则被简单地跳过:

pixels[positionOfPixel - offset + 1] == zero ? two : zero;

Sorry of it doesn't help, since I'm really not a C# coder)抱歉它没有帮助,因为我真的不是 C# 编码员)

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

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