C# 如何循环用户输入直到输入的数据类型正确?
如何使这段代码循环请求用户输入,直到 int.TryParse()
成功?
//setX
public void setX()
{
//take the input from the user
string temp;
int temp2;
System.Console.WriteLine("Enter a value for X:");
temp = System.Console.ReadLine();
if (int.TryParse(temp, out temp2))
x = temp2;
else
System.Console.WriteLine("You must enter an integer type value"); 'need to make it ask user for another input if first one was of invalid type'
}
有用答案后的代码版本:
//setX
public void setX()
{
//take the input from the user
string temp;
int temp2;
System.Console.WriteLine("Enter a value for X:");
temp = System.Console.ReadLine();
if (int.TryParse(temp, out temp2))
x = temp2;
else
{
Console.WriteLine("The value must be of integer type");
while (!int.TryParse(Console.ReadLine(), out temp2))
Console.WriteLine("The value must be of integer type");
x = temp2;
}
}
How to make this piece of code loop asking for input from the user until int.TryParse()
is successful?
//setX
public void setX()
{
//take the input from the user
string temp;
int temp2;
System.Console.WriteLine("Enter a value for X:");
temp = System.Console.ReadLine();
if (int.TryParse(temp, out temp2))
x = temp2;
else
System.Console.WriteLine("You must enter an integer type value"); 'need to make it ask user for another input if first one was of invalid type'
}
Version of the code after the helpful answer:
//setX
public void setX()
{
//take the input from the user
string temp;
int temp2;
System.Console.WriteLine("Enter a value for X:");
temp = System.Console.ReadLine();
if (int.TryParse(temp, out temp2))
x = temp2;
else
{
Console.WriteLine("The value must be of integer type");
while (!int.TryParse(Console.ReadLine(), out temp2))
Console.WriteLine("The value must be of integer type");
x = temp2;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:
试试这个。我个人更喜欢使用
while
,但do .. while
也是有效的解决方案。问题是我真的不想在任何输入之前打印错误消息。然而while
也存在无法将更复杂的输入推入一行的问题。这实际上取决于您到底需要什么。在某些情况下,我什至建议使用goto
,尽管有些人可能会因此追踪我并用鱼打我。edit:
Try this. I personally prefer to use
while
, butdo .. while
is also valid solution. The thing is that I don't really want to print error message before any input. Howeverwhile
has also problem with more complicated input that can't be pushed into one line. It really depends on what exactly you need. In some cases I'd even recommend to usegoto
even tho some people would probably track me down and slap me with a fish because of it.尽管问题已被标记为已回答,但
do-while
循环更适合验证用户输入。注意您的代码:
顶部和底部有相同的代码。这可以改变:
Even though the question has been already marked as answered,
do-while
loops are much better for validating user input.Notice your code:
You have the same code at top and bottom. This can be changed:
这也有帮助
This can help too
我一直想知道很多,但我刚刚弄清楚了!
该代码将循环,直到用户输入一个整数。这样,程序不会简单地报告错误,而是立即允许用户再次输入另一个正确的值。
I've been wondering quite a lot, but I just figured it out!
This code will loop until the user has entered an integer number. This way, the program doesn't simply report an error, but instead immediately allows the user to input again another, correct value.