2014-10-05 37 views
0

如何使用console.readline时检查字符串中是否有数字值?c#如何检查字符串是使用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?   
} 
+0

下次使用谷歌!在谷歌写这篇文章:c#如何检查字符串是一个数字。 – mybirthname 2014-10-05 05:14:32

回答

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); 
     }