C# 如何循环用户输入直到输入的数据类型正确?

发布于 2024-10-17 12:20:51 字数 1138 浏览 2 评论 0原文

如何使这段代码循环请求用户输入,直到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

身边 2024-10-24 12:20:51
while (!int.TryParse(Console.ReadLine(), out mynum))
    Console.WriteLine("Try again");

编辑:

public void setX() {
    Console.Write("Enter a value for X (int): ");
    while (!int.TryParse(Console.ReadLine(), out x))
        Console.Write("The value must be of integer type, try again: ");
}

试试这个。我个人更喜欢使用 while,但 do .. while 也是有效的解决方案。问题是我真的不想在任何输入之前打印错误消息。然而 while 也存在无法将更复杂的输入推入一行的问题。这实际上取决于您到底需要什么。在某些情况下,我什至建议使用goto,尽管有些人可能会因此追踪我并用鱼打我。

while (!int.TryParse(Console.ReadLine(), out mynum))
    Console.WriteLine("Try again");

edit:

public void setX() {
    Console.Write("Enter a value for X (int): ");
    while (!int.TryParse(Console.ReadLine(), out x))
        Console.Write("The value must be of integer type, try again: ");
}

Try this. I personally prefer to use while, but do .. while is also valid solution. The thing is that I don't really want to print error message before any input. However while 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 use goto even tho some people would probably track me down and slap me with a fish because of it.

情独悲 2024-10-24 12:20:51

尽管问题已被标记为已回答,但 do-while 循环更适合验证用户输入。

注意您的代码:

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");

顶部和底部有相同的代码。这可以改变:

do {
    Console.WriteLine("The value must be of integer type");
} while (!int.TryParse(Console.ReadLine(), out temp2));

Even though the question has been already marked as answered, do-while loops are much better for validating user input.

Notice your code:

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");

You have the same code at top and bottom. This can be changed:

do {
    Console.WriteLine("The value must be of integer type");
} while (!int.TryParse(Console.ReadLine(), out temp2));
幻想少年梦 2024-10-24 12:20:51

这也有帮助

public int fun()
{
    int Choice=0;
    try 
    {
        Choice = int.Parse(Console.ReadLine());
        return choice;
    } 
    catch (Exception) 
    {
        return fun(); 
    }
}

This can help too

public int fun()
{
    int Choice=0;
    try 
    {
        Choice = int.Parse(Console.ReadLine());
        return choice;
    } 
    catch (Exception) 
    {
        return fun(); 
    }
}
Spring初心 2024-10-24 12:20:51

我一直想知道很多,但我刚刚弄清楚了!

    int number;
    bool check;
    do
    {
        Console.WriteLine("Enter an integer:");
        check = int.TryParse(Console.ReadLine(), out num1);
    }
    while (!check);

该代码将循环,直到用户输入一个整数。这样,程序不会简单地报告错误,而是立即允许用户再次输入另一个正确的值。

I've been wondering quite a lot, but I just figured it out!

    int number;
    bool check;
    do
    {
        Console.WriteLine("Enter an integer:");
        check = int.TryParse(Console.ReadLine(), out num1);
    }
    while (!check);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文