检查String中的整数

时间:2016-10-30 18:41:12

标签: 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(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);
    }

但问题是当我输入char而不是int并且它工作正常并再次调用自己但是如果第二次或第二次之后我输入int然后不接受学生的输入而是它再次重复LOOP。 那么问题是输入函数????

中的问题

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式来查找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();
        }
}

答案 1 :(得分:-3)

我假设你是一名学生。我开始用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;
        }

    }