需要检查输入是否为小数

时间:2017-02-27 02:03:07

标签: c#

所以我在C#中为类编写控制台应用程序,我需要弄清楚如何检查输入是否为小数。它将遵循我到目前为止编写的代码:

Console.Write("Enter the annual amount of money saved: ");

decimal moneySaved = Convert.ToDecimal(Console.ReadLine());

在检查输入后,我希望它输出类似"您没有输入数字,请再试一次。"

提前致谢!

1 个答案:

答案 0 :(得分:0)

 Console.Write("Enter the annual amount of money saved: ");
 string moneySaved = Console.ReadLine();
 decimal number;
 if (Decimal.TryParse(moneySaved, out number))
 {
   Console.Write("You entered a decimal number");
 }
 else
 {
  Console.Write("You didn't enter a number, please try again.");
 }