Oh, 2 things: 1) It is a console application. 2 ) I know it is in danish, but it doesn't really matter, its just an example of asking for some input. The text and variables does not matter.
哦,2件事:1)这是一个控制台应用程序。 2)我知道它是丹麦语,但它并不重要,它只是一个要求输入的例子。文本和变量无关紧要。
Alright, consider this simple input: It could be any sort of input question really.
好吧,考虑一下这个简单的输入:它可能是任何类型的输入问题。
Console.WriteLine("Hvad er dit kundenummer: (Kun hele tal tilladt)");
string inputKnr = Console.ReadLine();
kundenummer = Convert.ToInt16(inputKnr);
Now, what if the customer types something wrong? Such as a letter. A try & catch would make sure the application does not break, but that is not the solution I want. I want it to say that you did it wrong, try again. Pretty classic right?
现在,如果客户输入错误怎么办?比如一封信。 try&catch会确保应用程序不会中断,但这不是我想要的解决方案。我希望它说你做错了,再试一次。很经典吧?
But what is the best way to solve this solution? I have thought of this:
但是解决这个解决方案的最佳方法是什么?我想到了这个:
bool fangetKundenummer = true;
while (fangetKundenummer)
{
Console.WriteLine("Hvad er dit kundenummer: (Kun hele tal tilladt)");
string inputKnr = Console.ReadLine();
try
{
kundenummer = Convert.ToInt16(inputKnr);
fangetKundenummer = false;
}
catch
{
Console.WriteLine("Fejl. Prøv igen");
}
}
But it just doesn't seem like the right way to do it.
但它似乎不是正确的方法。
Also, just to mention it, this little application I am playing with has 4 input questions in a row. This would mean 4 times this nasty while() loop.
另外,仅举这一点,我正在玩的这个小应用程序连续有4个输入问题。这意味着这个令人讨厌的while()循环的4倍。
You could also write a function. Something like this (no reason to do it the right way, its just to illustrate a concept):
你也可以写一个函数。像这样的东西(没有理由以正确的方式去做,它只是为了说明一个概念):
static void verifyInput()
{
try
{
Console.WriteLine("question");
input = Console.ReadLine();
kundenummer = Convert.ToInt16(input)
}
catch
{
Console.WriteLine("Wrong. Do it over");
verifyInput(); //start the function all over
}
}
But you'd have to write a function for each and every input question, even though they might ask exactly for the same! (meaning perhaps all asking for an integer; but with a different question and variable).
但是你必须为每个输入问题编写一个函数,即使它们可能会问同样的问题! (也许所有人都要求一个整数;但是有一个不同的问题和变量)。
This doesn't seem much better than the while() solution.
这似乎不比while()解决方案好多少。
Does anyone have a clever idea?
有没有人有一个聪明的主意?
4 个解决方案
#1
Use Int16.TryParse and the equivalents for other numeric types. All of these return a Boolean result to indicate success or failure for parsing, and take an out
parameter which is set to the result of the parsing (or 0 in case of failure). In your case you may want to wrap the call in a method to keep prompting:
使用Int16.TryParse和其他数字类型的等价物。所有这些都返回一个布尔结果来指示解析成功或失败,并取出一个out参数,该参数设置为解析的结果(或者在失败的情况下为0)。在您的情况下,您可能希望将方法中的调用包装起来以保持提示:
static Int16 PromptForInt16(string prompt)
{
while (true)
{
Console.Write(prompt);
Int16 result;
if (Int16.TryParse(Console.ReadLine(), out result))
{
return result;
}
Console.WriteLine("Sorry, invalid number entered. Try again.");
}
}
#2
You can use the TryParse pattern:
您可以使用TryParse模式:
string s; // for "is not valid" message
short val; // final value
while(!short.TryParse(s=Console.ReadLine(), out val)) {
Console.WriteLine(s + " is not valid...");
}
#3
Just for some variety, how about testing the string itself, instead of TryParse, which would require extra storage, and potentially an unneeded cast?
只是为了一些变化,如何测试字符串本身,而不是TryParse,这将需要额外的存储,并可能是一个不需要的演员?
static void Main(string[] args)
{
var isFalse = "t".IsInt();
var isTrue = "123".IsInt();
var isAlsoFalse = "123.1".IsInt();
}
static bool IsInt(this IEnumerable<char> s)
{
return s.All(x => char.IsNumber(x));
}
#4
Here's a link that might help:
这是一个可能有用的链接:
http://msdn.microsoft.com/en-us/library/system.int16.tryparse.aspx
#1
Use Int16.TryParse and the equivalents for other numeric types. All of these return a Boolean result to indicate success or failure for parsing, and take an out
parameter which is set to the result of the parsing (or 0 in case of failure). In your case you may want to wrap the call in a method to keep prompting:
使用Int16.TryParse和其他数字类型的等价物。所有这些都返回一个布尔结果来指示解析成功或失败,并取出一个out参数,该参数设置为解析的结果(或者在失败的情况下为0)。在您的情况下,您可能希望将方法中的调用包装起来以保持提示:
static Int16 PromptForInt16(string prompt)
{
while (true)
{
Console.Write(prompt);
Int16 result;
if (Int16.TryParse(Console.ReadLine(), out result))
{
return result;
}
Console.WriteLine("Sorry, invalid number entered. Try again.");
}
}
#2
You can use the TryParse pattern:
您可以使用TryParse模式:
string s; // for "is not valid" message
short val; // final value
while(!short.TryParse(s=Console.ReadLine(), out val)) {
Console.WriteLine(s + " is not valid...");
}
#3
Just for some variety, how about testing the string itself, instead of TryParse, which would require extra storage, and potentially an unneeded cast?
只是为了一些变化,如何测试字符串本身,而不是TryParse,这将需要额外的存储,并可能是一个不需要的演员?
static void Main(string[] args)
{
var isFalse = "t".IsInt();
var isTrue = "123".IsInt();
var isAlsoFalse = "123.1".IsInt();
}
static bool IsInt(this IEnumerable<char> s)
{
return s.All(x => char.IsNumber(x));
}
#4
Here's a link that might help:
这是一个可能有用的链接:
http://msdn.microsoft.com/en-us/library/system.int16.tryparse.aspx