复杂的 if else 逻辑
小伙伴们,
下面的复杂逻辑如何实现呢?
flag1 可以是“N”或“A”或“I”
flag2 可以是“N”或“A”或“I”
flag3 可以是“N”或“A”或“I”
函数(字符串flag1、字符串flag2、字符串flag3) begin
函数需要返回:
如果 flag1、flag2 和 flag3 为“N”,则返回“None”
否则返回“Active”如果flag1、flag2和flag3是“A”
- ,则返回“Active”
如果flag1、flag2和flag3是“I”,则返回“Inactive”
> 如果 flag1、flag2 和 flag3 为“A”和“I”(或“N”),则返回“both”
例如 1) flag1 是“A”,flag2 是“I”,flag3 是“I”
例如2)flag1是“I”,flag2是“A”,flag3是“I”
例如2)flag1是“A”,flag2是“N”,flag3是“I”
返回结果
结束
感谢您的回复,但没有一个帖子给出答案。我知道是否有其他构造并寻找逻辑来实现上述伪代码。所有四个都是可能的条件,特别是#4 很复杂,需要知道如何实现它。
Friends
How do I implement following complex logic?
flag1 can be "N" or "A" or "I"
flag2 can be "N" or "A" or "I"
flag3 can be "N" or "A" or "I"
function (string flag1, string flag2, string flag3)
begin
The function needs to return:
return "None" if flag1, flag2 and flag3 are "N"
else return "Active" if flag1, flag2 and flag3 are "A"
else return "Inactive" if flag1, flag2 and flag3 are "I"
else return "both" if flag1, flag2 and flag3 are either "A" AND "I" (OR "N")
e.g. 1) flag1 is "A" and flag2 is "I" and flag3 is "I"
e.g. 2) flag1 is "I" and flag2 is "A" and flag3 is "I"
e.g. 2) flag1 is "A" and flag2 is "N" and flag3 is "I"
retrun result
end
Thanks for reply but none of post gives answer. I know if else constrauct and looking for logic to implement above psedocode. All four are possibel conditions specially #4 is complex and need to know how to implement that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的第 4 点逻辑令人困惑...
我会为此使用枚举值,而不是字符串 - 它更加类型安全(例如,如果有人将
"WIBBLEWOBBLE"
传递给您的方法怎么办?它应该返回什么?)Your logic for point 4 is confusing...
I would use an enum value for this, rather than strings - it is much more type-safe (eg what if someone passed
"WIBBLEWOBBLE"
to your method? What should it return?)我认为如果您首先检查所有 3 个值是否相等,它既可以提高可读性,又可以提高速度。
I think it serves both readability and speed if you first check if all 3 values are equal.
这会有效。不能说我喜欢它。
This would* work. Can't say I like it.
Robaticus 给了你正确的答案,但是是在评论中而不是在帖子中,所以我将对其进行扩展。
我们有三面旗帜,可以分别代表三个州。因此有 3 * 3 * 3 = 27 个可能的选项。
当面对复杂的 if..then 逻辑和如此小的地址空间时,尝试编写所有 if..then 的代码是毫无意义的。相反,一个三维数组总共包含 27 个元素。
常量 int 无 = 0;
常量 int 不活动 = 1;
常量 int 活动 = 2;
私有 int ParseFlag(字符串标志)
{
开关(标志)
{
情况“N”:返回无;
情况“I”:返回Inactive;
情况“A”:返回活动;
default throw new Exception(string.Format("得到的标志值为 {0},但预期为 N、I 或 A", Flag));
}
}
public FlagResult Lookup(字符串Flag1, 字符串Flag2, 字符串Flag3)
{
返回 FlagData[ParseFlag(Flag1), ParseFlag(Flag2), ParseFlag(Flag3)];
我
会让你构建数组。
Robaticus gave you the right answer but in a comment rather than a post so I will expand upon it.
We have three flags that can take each of three states. There are thus 3 * 3 * 3 = 27 possible options.
When facing complex if..then logic and such a small address space it makes no sense at all to try to code all the if..then's. Instead, one three-dimensional array holding a total of 27 elements.
const int None = 0;
const int Inactive = 1;
const int Active = 2;
private int ParseFlag(string Flag)
{
switch (Flag)
{
case "N": return None;
case "I": return Inactive;
case "A": return Active;
default throw new Exception(string.Format("Got a flag value of {0} but expected N, I or A", Flag));
}
}
public FlagResult Lookup(string Flag1, string Flag2, string Flag3)
{
return FlagData[ParseFlag(Flag1), ParseFlag(Flag2), ParseFlag(Flag3)];
}
I'll let you build the array.