c#使用console.readline时如何检查字符串是否为数字
本文关键字:检查 字符串 是否 数字 console 使用 readline 何检查 | 更新日期: 2023-09-27 18:25:31
使用console.readline
时,如何检查字符串中是否有数值?
PlayerNode playerList = new PlayerNode();
for (int x = 1; x <= numPlayers; x++)
{
Console.WriteLine("What is your name?");
Player Aplayer = new Player();
Aplayer.Name = Console.ReadLine();
playerList.AddPlayer(Aplayer);
Console.WriteLine("How Much money do you want to play with?");
Aplayer.Winnings = float.Parse(Console.ReadLine());// How would i change this to check for number values?
}
在控制台中键入数字之前,它不会进入下一个语句。
Console.WriteLine("How much money do you want to play with?");
bool itsNumeric = false;
double money;
while(itsNumeric == false)
{
itsNumeric = double.TryParse(Console.ReadLine().ToString(), out money);
}
Decimal.TryParse(...)
用法:
decimal result;
if (Decimal.TryParse(str, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
CultureInfo.InvariantCulture, out result))
{
// ...
}
不要用浮点数来衡量财务价值。这个C#财务计算中的浮点问题在哪里?