解决了::三元运算符

编程入门 行业动态 更新时间:2024-10-27 08:36:38
本文介绍了解决了::三元运算符 - 我可以这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经搜索过,并且没有找到这种特殊语法的文档。 请考虑以下事项: 如果m_iPage确实为7,则StartLight设置为1,EndLight根据需要设置为4. 但是如果说m_iPage为0,则StartLight设置为0但EndLight为设置为4,其中我 真的需要将EndLight设置为0。

I''ve googled around and have not found documentation of this particular syntax. Consider the following: If m_iPage is indeed 7 then StartLight gets set to 1 and EndLight gets set to 4 as desired. However if m_iPage is say, 0, the StartLight gets set to 0 but EndLight is set to 4, where I really needed EndLight to be set to 0 also.

(m_iPage != 7) ? StartLight = m_iPage, EndLight = m_iPage : StartLight = 1, EndLight = 4;

这不正确? 感谢提前帮助

This is not correct? Thanks for the help in advance

推荐答案

虽然ProgramFOX的答案是正确的,但正是这种问题,建议坚持使用if / else语句。这将使代码更易于阅读和理解您正在做的事情,当您稍后再回来时,以及另一个开发人员,如果他们必须开始工作,那么这两个代码都是为了您自己。 此外,无论你是否接受这个建议,你最好还是以积极的形式(因为你在两种情况下都做了一些事情),因为如果这样做,做x,否则做y 总是比较简单的逻辑比如果不是这样,做x,否则做y。 即 While ProgramFOX''s answer is correct, it is precisely this sort of problem where it would be recommended to stick with an if/else statement. It will make the code easier to read and understand what you are doing, both for yourself when you come back to it later, as well as another developer if they have to take the work on. Furthermore, whether you take that advice or not, you really would be better off having your condition in the positive form (since you are doing something in both cases) because "if this, do x, otherwise do y" is always easier logic to follow than "if not this, do x, otherwise do y". I.e. (m_iPage == 7) ? (StartLight = 1, EndLight = 4) : (StartLight = m_iPage, EndLight = m_iPage);

但我再说一遍,如果可能的话,你真的应该改变这个:

But I repeat, you really should look to change this, if possible, to:

if (m_iPage == 7) { StartLight = 1; EndLight = 4; } else { StartLight = m_iPage; EndLight = m_iPage; }

问候, Ian。

Regards, Ian.

你必须写括号: Hi, You have to write parentheses: (m_iPage != 7) ? (StartLight = m_iPage, EndLight = m_iPage) : (StartLight = 1, EndLight = 4);

希望这有帮助。

Hope this helps.

更多推荐

解决了::三元运算符

本文发布于:2023-11-07 22:23:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   解决了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!