首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FormatException C#

FormatException C#
EN

Stack Overflow用户
提问于 2016-04-27 12:54:04
回答 3查看 73关注 0票数 1

因此,我目前正在上一门关于C#编程的课程,以便重新学习。

原来我忘了一些重要的事情!

代码语言:javascript
复制
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
            }
        }
    }
}
}

所以当你输入一个字符串而不是一个整数时,我试着让它说“这是无效的输入”。

提前谢谢!<3

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-27 12:57:33

您应该使用int.TryParse代替Parse,因为它具有内部异常处理机制,您可以使用它的返回值(true/false)来检查操作是否成功,如果转换成功,它将返回true,而失败转换的返回值将为false

代码语言:javascript
复制
int minutes;

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

Stack Overflow用户

发布于 2016-04-27 12:55:48

int minutes = int.Parse(input); -您应该使用TryParse()而不是Parse()

代码语言:javascript
复制
int minutes;
bool parsed = int.TryParse(input, out minutes);
if (parsed)
{
    // your if statements
}
else
{
    Console.WriteLine("That is not valid input");
}
票数 2
EN

Stack Overflow用户

发布于 2016-04-27 12:58:52

如果您正在接收来自用户的输入,则需要考虑实际使用Int32.TryParse()方法来确定解析是否成功:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36890455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档