C#如何循环用户输入,直到输入的数据类型是正确的? [英] C# How to loop user input until the datatype of the input is correct?

查看:951
本文介绍了C#如何循环用户输入,直到输入的数据类型是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使这块代码循环的要求来自用户的输入,直到的 int.TryParse()



是成功的?

  // setX的
公共无效setX的()
{
//采取从输入用户
线温度;
INT TEMP2;
的System.Console.WriteLine(用于输入X的值:);
TEMP = System.Console.ReadLine();
如果(int.TryParse(温度,出TEMP2))
X = TEMP2;
,否则
的System.Console.WriteLine(你必须输入一个整数类型值); 需要使它询问用户另一个输入,如果第一个是无效的类型为$ ​​B $ B}

有帮助的答案后的代码版本:

  // setX的
公共无效setX的()
{
//采取从用户
串温度的输入;
INT TEMP2;
的System.Console.WriteLine(用于输入X的值:);
TEMP = System.Console.ReadLine();
如果(int.TryParse(温度,出TEMP2))
X = TEMP2;
,否则
{
Console.WriteLine(值必须是整数类型);
,而
Console.WriteLine(值必须是整数类型)(int.TryParse(到Console.ReadLine(),出TEMP2)!);
X = TEMP2;
}
}


解决方案

 ,而
Console.WriteLine(重试)(int.TryParse(到Console.ReadLine(),出来的myNum)!);



编辑:

 公共无效setX的(){
Console.Write(输入X(int类型的数值):);
,而
Console.Write(值必须是整数类型,再试一次:)(int.TryParse(到Console.ReadLine(),出X)!);
}



试试这个。我个人更喜欢使用,而,但做..而也是有效的解决方案。问题是,我真的不希望打印错误信息的的任何输入。然而,而也有不能推入一​​行更复杂的输入问题。这真的取决于正是你需要的。在某些情况下,我甚至会推荐使用转到即使寿有些人可能会跟踪我,并拍我,因为它一条鱼。


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;
        }
    }

解决方案

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.

这篇关于C#如何循环用户输入,直到输入的数据类型是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆