C# 有没有一种好方法可以使用 Console.ReadKey 在值之间进行选择而无需在类型之间进行大量转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/648555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a good way to use Console.ReadKey for choosing between values without doing a lot of conversion between types?
提问by Patrik Bj?rklund
Im using Console.ReadKey() to choose from a number of options that varies from time to time. Before this initial code snippet there is a for loop that counts occurances into counter variable of type int.
我使用 Console.ReadKey() 从许多不时变化的选项中进行选择。在此初始代码片段之前,有一个 for 循环,用于将出现次数计数为 int 类型的计数器变量。
The point is to use Console.ReadKey() to get an int.
关键是使用 Console.ReadKey() 来获取 int。
int choice = ReadKey();
Console.WriteLine("");
if (choice < counter)
{
mail.to = result[counter-1].email;
}
By using the following methods
通过使用以下方法
static int ReadKey()
{
ConsoleKeyInfo choice = Console.ReadKey();
char convertedchoice = choice.KeyChar;
string convertedchoice2 = convertedchoice.ToString();
int result = TryInt(convertedchoice2);
return result;
}
static int TryInt(string totry)
{
while (true)
{
int result;
if (int.TryParse(totry, out result))
{
return result;
}
Console.WriteLine("Sorry, you need to enter a number. Try again.");
}
}
I tried using ToString() but this was the way that it would let me do it in the end. So this looks kind of inneffective to me and hence I would really appreciate some guidance as what to do differently?
我尝试使用 ToString() 但这是它最终让我这样做的方式。所以这对我来说看起来有点无效,因此我真的很感激一些关于如何做不同的指导?
Edit:
编辑:
I ended up with a combination of all the good answers below. Thanks alot guys.
我最终得到了下面所有好的答案的组合。非常感谢各位。
static int ReadKey()
{
while (true)
{
ConsoleKeyInfo choice = Console.ReadKey();
if (char.IsDigit(choice.KeyChar))
{
int answer = Convert.ToInt32(choice.KeyChar);
return answer - 48; //-48 because 0 is represented in unicode by 48 and 1 by 49 etc etc
}
Console.WriteLine("\nSorry, you need to input a number");
}
}
采纳答案by Henk Holterman
For a menu system with choices 0..9 this is reasonably OK. Not for reading larger numbers though.
对于具有选项 0..9 的菜单系统,这是合理的。不是为了读取更大的数字。
Your whole checking logic can be made a lot easier with char.IsDigit() :
使用 char.IsDigit() 可以使您的整个检查逻辑更容易:
if char.IsDigit(convertedchoice)
{
int result = convertedchoice - '0'; // char1 - char2 = int, in this case in 0..9
return result;
}
else ...
回答by Reed Copsey
You can just call Convert.ToInt32(choice.KeyChar);
directly.
Convert.ToInt32(choice.KeyChar);
直接打电话就可以了。
That would simplify it a bit.
那会稍微简化一下。
回答by MarkusQ
There are lots of ways to simplify your code, but for a start try to avoid putting everything into a variable. In general, things like:
有很多方法可以简化您的代码,但首先尝试避免将所有内容都放入变量中。一般来说,事情像:
(a + b + c) / 2
are much easier to read than things like:
比以下内容更容易阅读:
int A_plus_B = a + b
int A_plus_B_plus_C = A_plus_B + c
int answer = A_plus_B_plus_C / 2
With this in mind, you could write:
考虑到这一点,你可以写:
static int ReadKey()
{
while (true)
{
char ch = Console.ReadKey().KeyChar;
int result;
if (int.TryParse(ch.toString(), out result))
{
return result;
}
}
}