2014-10-05 34 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); 
     }