0

使用时如何检查字符串中是否有数字值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?        
}
4

2 回答 2

1

在控制台中输入数字之前,它不会进入下一条语句。

        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);
        }                
于 2014-10-05T05:08:22.220 回答
0

Decimal.TryParse(...)

用法:

decimal result;
if (Decimal.TryParse(str, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                     CultureInfo.InvariantCulture, out result))
{
    // ...
}

不要将浮点数用于财务价值。这个 C# 财务计算中的浮点问题在哪里?

于 2014-10-05T04:53:37.877 回答