如何通过使用“?”来缩小这个特定的 if 行(混乱)操作员
我正在开发一个项目,我必须查看别人的代码并对其进行修改。 然而,由于我班上的很多同学都是编程新手,所以很多人的组织都很混乱。 我被指派临时编写的代码有很多缺陷和凌乱的冗余行。 我正在尽力清理它们,但是由于我的经验不足,我发现清理它们很难。 诸如此类的线条
if (turnElapsed[1] == 2)
{
turnElapsed[0] += 1;
turnElapsed[1] = 0;
}
turnElapsed[1]++;
对我来说看起来相当多余。 我相信,并且一定有更好的方法来编写它的简单版本。 所以我尝试了下面的代码,但它似乎无法正常工作。
turnElapsed[0] += (turnElapsed[1]++ == 2) ? 1 ; 0 ;
turnElapsed[1] = (turnElapsed[1] == 2 ) ? 0; turnElapsed[1];
I am working on a project where I have to look into someone else's code and modify it.
However since many classmates in my class are quite new to program, many have such a messy organization.
The code that I am assigned to improvise has a lot of flaws and messy redundant lines.
I am trying my best to clean them up, however due to my inexperience, I find it hard to clean them up.
Lines such as
if (turnElapsed[1] == 2)
{
turnElapsed[0] += 1;
turnElapsed[1] = 0;
}
turnElapsed[1]++;
looks quite redundant to me.
I believe, and there must be a better way to write a simple version of it.
so I tried the code below but it seemed to not work properly.
turnElapsed[0] += (turnElapsed[1]++ == 2) ? 1 ; 0 ;
turnElapsed[1] = (turnElapsed[1] == 2 ) ? 0; turnElapsed[1];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您使用
;
作为分隔符,而不是:
,这是一个语法错误。其次,您在第一行增加turnElapsed[1]
,这意味着当它到达第二行时,它将不再等于 2 - 这与原始逻辑不同。这就是您的版本无法正常工作的原因。但是,如果您确实修复了这些错误,我认为您的代码不会更容易阅读。原文更具可读性,因为它更清楚地表达了意图。您可以阅读该内容并将其表述为“如果turnElapsed[1] 为2,则...”。您的替代方案需要更少的行数,但更“神秘”。原始代码的另一个优点是,如果您想在 if 条件为 true 时中断,可以在大括号内放置一个断点 - 您不能使用三元运算符 (?) 来做到这一点。
Firstly, you are using
;
as separator instead of:
which is a syntax error. Secondly, you're incrementingturnElapsed[1]
on the first line, which means that when it reaches the second line it will no longer equal 2 - that's different to the original logic. This is why your version does not work properly.However, if you did fix those errors I don't think your code would be easier to read. The original is more readable, because it expresses the intention more clearly. You can read that and verbalise it as "if turnElapsed[1] is 2 then ...". Your alternative takes fewer lines, but is more "cryptic". Another advantage of the original code is that you can put a breakpoint inside the braces if you wanted to break when the if condition was true - you cannot do that with the ternary operator (?).
你的问题是它不可读和不可理解。这就是为什么你看不到你的错误。
错误在于您不会在第二个版本中增加。试试这个
Your problem is that it is not readable and comprehensible. That is why you don't see your bug.
The bug is that you don't increment in the second version. Try this
难道你不只是在turnElapsed中保留一个二进制数,而最高有效位在turnElapsed[0]中吗?代码的更好版本是:
或者,如果您真的不想开始搞乱位:
编辑: 显然,turnElapsed[1] 是 0 或 1,并且每次turnElapsed 从 1 变化时,turnElapsed[0] 都会递增到 0。所以你有以下内容:
不需要 ifs、?:'s 或其他任何东西。事实上,您甚至不需要该数组。
Aren't you just keeping a binary number in turnedElapsed, with the most significant bit in turnElapsed[0]? A much better version of the code would be:
Or, if you really don't want to start messing with bits:
EDIT: Apparantly turnElapsed[1] is 0 or 1, and turnElapsed[0] is incremented every time turnElapsed changes from 1 to 0. So you have the following:
No need for ifs, ?:'s or anything else. In fact, you don't even need the array.