int.Parse不使用字符串值(System.FormatException:输入字符串的格式不正确)

时间:2017-12-05 04:27:42

标签: c# .net

如果用户输入的数字不是数字,我会得到未处理的异常:

  

System.FormatException:输入字符串的格式不正确。

有人可以解释我做错了吗?

这是我的代码C#.NET:

private static int WeightIFeMale()
{
    Console.WriteLine("How Much Do You Weigh?", Environment.NewLine);
    int femaleWeight = int.Parse(Console.ReadLine());

    if 
        (0 >= femaleWeight && 80 <= femaleWeight)
    {
        Console.WriteLine("Damn, how much crack are you smoking?", Environment.NewLine);
    }
    else if
        (81 >= femaleWeight && 110 <= femaleWeight)
    {
        Console.WriteLine("What the ****, are you a midget *****?", Environment.NewLine);

    }        
    else if 
        (111 >= femaleWeight && 140 <= femaleWeight)
    {
        Console.WriteLine("Holy shit, I have some protein bar coupons you can have!", Environment.NewLine);

    }
    else if 
        (141 >= femaleWeight && 160 <= femaleWeight)
    {
        Console.WriteLine("Bro you need to hit the Gym, ASAP!", Environment.NewLine);

    }
    else if 
        (161 >= femaleWeight && 200 <= femaleWeight)
    {
        Console.WriteLine("Really!!!, can I hit the Gym with you?", Environment.NewLine);

    }
    else if 
        (201 >= femaleWeight && 240 <= femaleWeight)
    {
        Console.WriteLine("Do you Have a small ****, Steroids are a mother @#$%^&&", Environment.NewLine);

    }
    else if 
        (241 >= femaleWeight && 999 <= femaleWeight)
    {
        Console.WriteLine("My condolences!", Environment.NewLine);
    }
    else if 
        (femaleWeight >= 1000)
    {
        Console.WriteLine("There is absolutely no way that you weigh {0} lbs, so stop ******* around!", femaleWeight, Environment.NewLine);
    }
    else
    {
        Console.Write("What the **** is {0} It has to be a number numb nuts, lets try that again!", Environment.NewLine);
        WeightIFeMale();
    }

    return femaleWeight;
}

4 个答案:

答案 0 :(得分:1)

这里当你提供的值似乎不是数值时int.Parse()不能将它解析为INT。请参阅此文Int32.Parse Method (String)并查看例外表 FormatException说明。所以,1。你有更好的尝试捕获功能。尝试以下建议,它正在工作。 2.您也可以使用int.TryParse方法

方式1。使用try catch语句删除此语句int femaleWeight = int.Parse(Console.ReadLine());

   private static int WeightIFeMale()

        {
            Console.WriteLine("How Much Do You Weigh?", Environment.NewLine);
            //int femaleWeight = int.Parse(Console.ReadLine());
            string weigt_st = Console.ReadLine();
            int femaleWeight = 0;
            try
            {
                femaleWeight = int.Parse(weigt_st);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            if (0 >= femaleWeight && 80 <= femaleWeight)
            {
                Console.WriteLine("Damn, how much crack are you smoking?", Environment.NewLine);
            }
            else if (81 >= femaleWeight && 110 <= femaleWeight)
            {
                Console.WriteLine("What the ****, are you a midget *****?", Environment.NewLine);

            }

            else if (111 >= femaleWeight && 140 <= femaleWeight)
            {
                Console.WriteLine("Holy shit, I have some protein bar coupons you can have!", Environment.NewLine);

            }

            else if (141 >= femaleWeight && 160 <= femaleWeight)
            {
                Console.WriteLine("Bro you need to hit the Gym, ASAP!", Environment.NewLine);

            }
            else if (161 >= femaleWeight && 200 <= femaleWeight)
            {
                Console.WriteLine("Really!!!, can I hit the Gym with you?", Environment.NewLine);

            }
            else if
                (201 >= femaleWeight && 240 <= femaleWeight)
            {
                Console.WriteLine("Do you Have a small ****, Steroids are a mother @#$%^&&", Environment.NewLine);

            }
            else if
                (241 >= femaleWeight && 999 <= femaleWeight)
            {
                Console.WriteLine("My condolences!", Environment.NewLine);
            }
            else if
                (femaleWeight >= 1000)
            {
                Console.WriteLine("There is absolutely no way that you weigh {0} lbs, so stop ******* around!", femaleWeight, Environment.NewLine);
            }
            else
            {
                Console.Write("What the **** is {0} It has to be a number numb nuts, lets try that again!", Environment.NewLine);
                WeightIFeMale();
            }

            return femaleWeight;
        }

或方式2。使用此代码删除此int femaleWeight = int.Parse(Console.ReadLine());代码

   int femaleWeight = 0;
   int.TryParse(Console.ReadLine(), out femaleWeight); 

答案 1 :(得分:0)

由于int femaleWeight = int.Parse(Console.ReadLine());

如果输入无法转换为整数,

int.Parse方法将抛出FormatException

答案 2 :(得分:0)

int femaleWeight;

if (!int.TryParse(Console.ReadLine(), out femaleWeight)) {
   Console.WriteLine("Please enter your weight in US pounds")
}
else if (...)

答案 3 :(得分:0)

This is how it worked for me:

private static int WeightIFeMale()
    {
            Console.WriteLine("How Much Do You Weigh?", Environment.NewLine);
    } 
 int femaleWeight;

            if (!int.TryParse(Console.ReadLine(), out femaleWeight)) //I deleted the else statement.
    {
    Console.Write("What the **** is {0} It has to be a number numb nuts, lets try that again!", Environment.NewLine);
        Console.Write(Environment.NewLine);
        WeightIFeMale();
    }
    else if 
        (femaleWeight >= 0   && femaleWeight <= 80)
    {
        Console.WriteLine("Damn, how much crack are you smoking?", Environment.NewLine);
    }
    else if
        (femaleWeight >= 81 && femaleWeight <= 110)
    {
        Console.WriteLine("What the ****, are you a midget *****?", Environment.NewLine);

    }        
    else if 
        (femaleWeight >= 111 && femaleWeight <= 140)
    {
        Console.WriteLine("Holy ***, I have some protein bar coupons you can have!", Environment.NewLine);

    }
    else if 
        (femaleWeight >= 141 && femaleWeight <= 160)
    {
        Console.WriteLine("Bro you need to hit the Gym, ASAP!", Environment.NewLine);

    }
    else if 
        (femaleWeight >= 161 && femaleWeight <= 200)
    {
        Console.WriteLine("Really!!!, can I hit the Gym with you?", Environment.NewLine);

    }
    else if 
        (femaleWeight >= 201 && femaleWeight <= 240)
    {
        Console.WriteLine("Do you Have a small ****, Steroids are a mother @#$%^&&", Environment.NewLine);

    }
    else if 
        (femaleWeight >= 241 && femaleWeight <= 999)
    {
        Console.WriteLine("My condolences!", Environment.NewLine);
    }
    else if 
        (femaleWeight >= 1000)
    {
        Console.WriteLine("There is absolutely no way that you weigh {0} lbs, so stop ******* around!", femaleWeight, Environment.NewLine);
    }