在枚举中使用按位或
我需要在控制台应用程序中处理用户的输入,我只需要允许z字段编号(...,-1、0、1,...)。
我通过保留最后一个字符可以验证订单是正确的。最后,用户必须输入z号的集合,例如-3 6 7 101 -500
。
我的问题是与lastinput
的比较为空间| ...
请查看代码。
public void Foo()
{
ConsoleKeyInfo key;
var chars = new List<char>();
NextChar last = NextChar.Space;
var list = new List<NextChar> {last};
do
{
key = Console.ReadKey(true);
NextChar next = ProcessCharInput(key.KeyChar);
switch (next)
{
case NextChar.None:
if(key.Key != ConsoleKey.Enter)
{
return;
}
continue;
case NextChar.Space:
if (last == (NextChar.Numeric))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
case NextChar.Minus:
if (last == (NextChar.Space))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
case NextChar.Numeric:
if (last == (NextChar.Numeric | NextChar.Minus | NextChar.Space))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
while (true);
}
[Flags]
private enum NextChar
{
None = 0x0,
Space = 0x1,
Minus = 0x2,
Numeric = 0x4
}
我猜我在枚举中做错了什么,因为数字
和最后一个是space
我无法获得last ==(NextChar.numeric | NextChar.Minus | NextChar.Space)
是正确的。
I need to process the input from the user in a console application, and i need to allow only Z field numbers (... ,-1 , 0 , 1 ,...).
I have built that process char by char from the user, by keeping the last char i can verify that the order is correct. in the end the user must enter set of Z numbers for example-3 6 7 101 -500
.
My problem is with the comparison of the LastInput
as enum
, meaning i whant to check if the last input was Numeric | Space | ...
please take a look at the code.
public void Foo()
{
ConsoleKeyInfo key;
var chars = new List<char>();
NextChar last = NextChar.Space;
var list = new List<NextChar> {last};
do
{
key = Console.ReadKey(true);
NextChar next = ProcessCharInput(key.KeyChar);
switch (next)
{
case NextChar.None:
if(key.Key != ConsoleKey.Enter)
{
return;
}
continue;
case NextChar.Space:
if (last == (NextChar.Numeric))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
case NextChar.Minus:
if (last == (NextChar.Space))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
case NextChar.Numeric:
if (last == (NextChar.Numeric | NextChar.Minus | NextChar.Space))
{
Console.Write(key.KeyChar);
last = next;
chars.Add(key.KeyChar);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
while (true);
}
[Flags]
private enum NextChar
{
None = 0x0,
Space = 0x1,
Minus = 0x2,
Numeric = 0x4
}
I am guessing that i am doing something wrong with the enum because the input of Numeric
and last is Space
i cant get the last == (NextChar.Numeric | NextChar.Minus | NextChar.Space)
to be true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定要进行按位“或”运算吗?
这似乎没有帮助。您似乎正在尝试在这里执行布尔逻辑。在这种情况下,您需要将
last == (NextChar.Numeric | NextChar.Minus | NextChar.Space)
更改为
...当然,老实说,如果您受限于一组4 个值,并且您试图确保对其中 3 个值执行某些操作,您可能会更清晰、更具表现力
Are you sure you're trying to do a bitwise OR?
Which doesn't seem to help. It seems likely that you're trying to do boolean logic here. In which case, you'd want to change
last == (NextChar.Numeric | NextChar.Minus | NextChar.Space)
to
...of course, honestly, if you're constrained to a set of 4 values and you're trying to make sure that you do something on 3 of them, you're probably more clear and expressive with
以下几行说明了如何解释这一点:
您想要的可能是类似:
或按位逻辑:
The following lines illustrate how this will be interpreted:
What you want is probably something like:
or with bitwise logic:
用于位标志的一般模式是
; <操作> (值和掩码))
。针对您的具体情况:if(NextChar.None != (last & (NextChar.Numeric | NextChar.Minus | NextChar.Space))) { ... }
The general pattern you want to use for bit flags is
<comparison value> <operation> (value & mask))
. For your case specifically:if(NextChar.None != (last & (NextChar.Numeric | NextChar.Minus | NextChar.Space))) { ... }