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

前端 未结 2 1056
予麋鹿
予麋鹿 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.

    0 讨论(0)
  • 2021-01-23 09:23

    In console application you can't control text input (no keypressed event to handle).

    This could be a solution

        Console.WriteLine("Inesrt your gold: ");
        int gold;
        while(!int.TryParse(Console.ReadLine(),out gold)){
        Console.WriteLine("Please provide a valid gold value");
        }
        Console.WriteLine("Your gold is : " + gold);
    
    0 讨论(0)
提交回复
热议问题