捕获输入错误

时间:2014-04-01 02:24:34

标签: c# exception

我有学校的这个任务,我们必须使用try和catch,以确保用户不输入负数的甜甜圈和非整数的数据。我得到了try / catch工作,以便在我输入负数时再次询问甜甜圈的数量,但是当我输入一个字母而不是一个数字来表示甜甜圈的数量时,它会出现我为我创建的错误消息它但不提供再次输入甜甜圈数量的选项。如果有人能帮助我,我将不胜感激。谢谢。这是我的代码所得到的:

using System;
public class CostofDonuts
{
    public static void Main()
    {
        string lastName;
        int number_Of_Donuts;
        double Total_Cost, Final_Cost;


        try
        {

            // Get user to input their last name
            Console.Write("Enter customer's last name -> ");
            lastName = Convert.ToString(Console.ReadLine());

            //Get user to input amount of donuts purchased. Ensure that the integer inputted is positive.
            do
            {
                Console.Write("Enter the amount of donuts purchased -> ");
                number_Of_Donuts = Convert.ToInt32(Console.ReadLine());
                if (number_Of_Donuts < 0)
                    Console.WriteLine("Invalid input, number of donuts must be positive");
            } while (number_Of_Donuts <= 0);

            //Calculate cost of donuts
            if (number_Of_Donuts < 6)
                Total_Cost = number_Of_Donuts * 0.5;
            if (number_Of_Donuts <= 15)
                Total_Cost = number_Of_Donuts * 0.4;
            else
                Total_Cost = number_Of_Donuts * 0.3;

            //Calculate cost with tax
            if (number_Of_Donuts < 12)
                Final_Cost = (Total_Cost + 0.25) * 1.13;
            else
                Final_Cost = Total_Cost + 0.25;

            // Output final results
            Console.WriteLine("{0} bought {1} donuts which came to a total of {2:C}", lastName, number_Of_Donuts, Final_Cost);
            Console.ReadLine();
        }



        catch (FormatException e)
        {
            Console.WriteLine("Input must be a positive integer");
        }

        catch (Exception e)
        {
            Console.WriteLine("Input must be a positive integer");
        }
    }
}

3 个答案:

答案 0 :(得分:2)

您需要在循环中使用try / catch才能保持继续

do
{
      Console.Write("Enter the amount of donuts purchased -> ");
      try
      {
           number_Of_Donuts = Convert.ToInt32(Console.ReadLine());
      }
      catch (Exception)
      {
           Console.WriteLine("Invalid input, number of donuts must be positive");
           number_Of_Donuts = 0;
      }

} while (number_Of_Donuts <= 0);

答案 1 :(得分:0)

您可以使用int.TryParse

   do
    {
        Console.Write("Enter the amount of donuts purchased -> ");
        if(int.TryParse(Console.ReadLine(), out number_Of_Donuts ) && number_Of_Donuts >= 0)
            break;
        Console.WriteLine("Invalid input, number of donuts must be positive");
    } while (number_Of_Donuts <= 0);

答案 2 :(得分:0)

       do
        {
            Console.Write("Enter the amount of donuts purchased -> ");
            Int.TryParse(Console.ReadLine(), out number_Of_Donuts);
            if (number_Of_Donuts < 0)
                Console.WriteLine("Invalid input, number of donuts must be positive");
        } while (number_Of_Donuts <= 0);