简体   繁体   中英

FormatException C#

so I'm currently taking a course on C# programming to get freshened up.

Turned out I forgot some important things!

namespace FitnessFrog
{
class Program
{
    static void Main()
    {
        int runningTotal = 0;

        bool keepGoing = true;

        while(keepGoing)
        {

            // Prompt the user for minutes exercised
            Console.Write("Enter how many minutes you exercised or type \"quit\" to exit: ");
            string input = Console.ReadLine();

            if (input == "quit")
            {
                keepGoing = false;
            }
            else
            {
                try
                {
                    int minutes = int.Parse(input);
                    runningTotal = runningTotal + minutes;

                    if(minutes <= 0)
                    {
                       Console.WriteLine("Eh, what about actually exercising then?"); 
                        continue;
                    }
                    else if(minutes <= 10)
                    {
                        Console.WriteLine("Better than nothing, am I right?");
                    }
                    else if (minutes <= 24)
                    {
                        Console.WriteLine("Well done, keep working!");
                    }
                    else if (minutes <= 60)
                    {
                        Console.WriteLine("An hour, awesome! Take a break, ok?");
                    }
                    else if (minutes <= 80)
                    {
                        Console.WriteLine("Woah, remember to drink if you're going to exercise THAT long!");
                    }
                    else
                    {
                        Console.WriteLine("Okay, now you're just showing off!");
                    }
                    Console.WriteLine("You've exercised for " + runningTotal + " minutes");
                }
                    catch(FormatException)
                    {
                        Console.WriteLine("That is not valid input");
                        continue;
                    }




                // Repeat until the user quits
            }
        }
    }
}
}

So I'm trying to make it say "This is not valid input" when you type a string instead of an integer.

Thanks in advance! <3

int minutes = int.Parse(input); - you should use TryParse() instead of Parse()

int minutes;
bool parsed = int.TryParse(input, out minutes);
if (parsed)
{
    // your if statements
}
else
{
    Console.WriteLine("That is not valid input");
}

You should use int.TryParse instead for Parse, since it having internal exception handling mechanisms, And you can use it's return value(true/false)to check whether the operation is successful or not, for a successful conversion it will return true, and the return value for a failure conversion will be false

int minutes;

if(!int.TryParse(input,out minutes)
{
    Console.WriteLine("invalid input");
}
else
{
   // Proceed
}

If you are receiving input from your users, you will want to consider actually using the Int32.TryParse() method to determine if the parse was successful or not :

int minutes;
// Attempt the parse here
if(Int32.TryParse(input, out minutes))
{
    // The parse was successful, your value is stored in minutes
}
else 
{
    // The parse was unsuccessful, consider re-prompting the user
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM