捕获输入错误

本文关键字:错误 输入 | 更新日期: 2023-09-27 17:55:29

我有这个学校作业,我们必须使用 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");
        }
    }
}

捕获输入错误

你需要

在循环中使用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);

你可以使用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);
       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);