C# 检查字符串中的整数

C# 检查字符串中的整数,c#,C#,我正在检查字符串在函数Parse()中是否包含整数或其他内容。 这是我的密码 static public int input() { Console.WriteLine("Enter The Number Of Student You Want to get Record"); int x; string inputString = Console.ReadLine(); if (int.TryParse(inputStri

我正在检查字符串在函数Parse()中是否包含整数或其他内容。 这是我的密码

static public int input()
    {
        Console.WriteLine("Enter The Number Of Student You Want to get Record");
        int x;
        string inputString = Console.ReadLine();
        if (int.TryParse(inputString, out  x))
        {
            Console.WriteLine(inputString + " Is Integer");
            return x= Convert.ToInt32(inputString);
        }
        else
        {
            input();
        }
        return x;

    }
完整代码为:

        static void Main(string[] args)
    {
        int num = 0;
        string[] names = new string[] { };
        long[] fee = new long[] { };
        string[] className = new string[] { };

        do
        {
            Console.WriteLine("Enter Option You Want: \nA:Enter Student Record\nB:Display Student Record\nQ:Exit");
            string option =null;
            option =Console.ReadLine();
            switch (option)
            {
                case "A":
                case "a":
                   {

                        num = input();
                        names = new string[num];
                        fee = new long[num];
                        className = new string[num];
                        for (int i = 0; i < num; i++)
                        {
                            Console.WriteLine("Enter Name Of Student:{0}",i); 
                            Console.Write("Enter Student Name: "); names[i] = Console.ReadLine();
                            Console.Write("Enter Student Fee: "); fee[i] = Convert.ToInt32(Console.ReadLine());
                            Console.Write("Enter Student Class Name: "); className[i] = Console.ReadLine();
                        }
                        break;
                    }
                case "B":
                case "b":
                    {

                        for (int i = 0; i < names.Length; i++)
                        {
                            Console.WriteLine("Record of Student: {0}",i);
                            Console.WriteLine("Name: "+names[i]+ "\nFee: " + fee[i]+ "\nClass Name: " + className[i]);
                            //Console.WriteLine("Name: {0}\n Class Name: {1}\n Fee: {3}\n",names[i],className[i],fee[i]);
                        }

                        break;
                    }
                case "Q":
                case "q":
                    {
                        Environment.Exit(1);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Invalid Option");
                        break;
                    }

            }
        } while (true);
    }
static void Main(字符串[]args)
{
int num=0;
字符串[]名称=新字符串[]{};
long[]费=新long[]{};
string[]className=新字符串[]{};
做
{
Console.WriteLine(“输入您想要的选项:\nA:Enter Student Record\nB:Display Student Record\nQ:Exit”);
字符串选项=null;
option=Console.ReadLine();
开关(选件)
{
案例“A”:
案例“a”:
{
num=输入();
名称=新字符串[num];
费用=新长[num];
className=新字符串[num];
for(int i=0;i
但问题是,当我输入char而不是int时,它工作正常并再次调用自己,但如果第二次或第二次之后我输入int,则不接受学生的输入,而是再次重复循环。
那么问题是什么,是输入函数的问题???

我假设你是个学生。我从C#开始做同样的事情。我不会再这么做了,但既然你这么做了。我建议使用goto,让这个方法递归是不行的

static public int input()
    {
     Prompt:
        Console.WriteLine("Enter The Number Of Student You Want to get Record");
        int x;
        string inputString = Console.ReadLine();
        if (int.TryParse(inputString, out  x))
        {
            Console.WriteLine(inputString + " Is Integer");
            return x;
        }
        else
        {
            goto Prompt;
        }

    }

您可以使用正则表达式来查找int。你也应该打电话

return input(); 
而不是

input();
新方法:

static public int input(){
       Console.WriteLine("Enter The Number Of Student You Want to get Record");
        string input = Console.ReadLine();

        if (Regex.IsMatch(input, @"\d+"))
        {
            return int.Parse(Regex.Match(input, @"\d+").Value);
        }
        else
        {
            return input();
        }
}

返回输入()而不是只调用
input()谢谢,它成功了。不使用它会有什么问题???永远不要在像C这样的高级语言中使用goto,这里的goto比递归调用好得多。人们说,永远不要在C#中使用Goto。。好吧,回答我这个问题,为什么用这种语言呢?Goto实际上对Console.Writeline提示非常有用。无限循环是
Goto
@Proximo的一个很好的替代方法,因为开关案例在内部被视为Goto。它在极少数情况下也很有用,但这次肯定不行。在C#中声明类中的数据成员时,很高兴能帮助确定您需要什么。如果我想声明内置对象,那么我应该只创建指针还是整个对象,即DateTime DOB或DateTime DOB=new DateTime();在类中声明时??将其声明为Datetime DOB,但在构造函数中对其进行初始化。如果不进行初始化,则将收到空指针异常