How do I only allow number input into my C# Console Application?

后端 未结 6 2109
鱼传尺愫
鱼传尺愫 2020-11-28 12:54
Console.WriteLine(\"Enter the cost of the item\");                           
string input = Console.ReadLine();
double price = Convert.ToDouble(input);
6条回答
  •  眼角桃花
    2020-11-28 13:33

            string input;
            double price;
            bool result = false;
    
            while ( result == false )
                {
                Console.Write ("\n Enter the cost of the item : ");
                input = Console.ReadLine ();
                result = double.TryParse (input, out price);
                if ( result == false )
                    {
                    Console.Write ("\n Please Enter Numbers Only.");
                    }
                else
                    {
                    Console.Write ("\n cost of the item : {0} \n ", price);
                    break;
                    }
                }
    

提交回复
热议问题