Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED

前端 未结 2 1101
予麋鹿
予麋鹿 2021-01-23 08:51

I have an application that calculates your gold per minute farmed. I want to make an error report that wouldn\'t allow you to insert any text in the console input where yous hou

2条回答
  •  我在风中等你
    2021-01-23 09:13

    Instead of using int.Parse, you can use int.TryParse, and print a message:

    int gold;
    while(!int.TryParse(Console.ReadLine(), out gold))
    {
        Console.WriteLine("Please enter a valid number for gold.");
        Console.WriteLine("Inesrt your gold: ");
    }
    

    This allows you to re-prompt and handle errors correctly.

提交回复
热议问题