问题是:

该应用程序应询问用户要预订的门票总数。在预订机票时,如果预订的机票总数超过了可用机票,则应用程序应提出例外。我不知道为什么这样做时没有显示错误,所以提出了以下解决方案:

using System;
namespace Ticket
{
    class blah
    {
        public void abc()
        {
            int numberOfTickets;
            int numberOfAvailableTickets=10;
            int cost = 100;
            int pay;
            Console.WriteLine("how many tickets do you need");
            numberOfTickets = Convert.ToInt32(Console.ReadLine());

            try
            {
                if (numberOfTickets < numberOfAvailableTickets)
                {
                    pay = 100 * numberOfTickets;
                    Console.WriteLine("Pay please");
                    Console.WriteLine(pay);
                }
            }
            if( numberOfTickets>numberOfAvailableTickets)
            {
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
            }
        }
    }

}

 class Theater
 {
        static void Main(string[] args)
        {
            blah hi = new blah();
            hi.abc();
            Console.ReadLine();
        }
  }
}

最佳答案

我什至不确定您显示的代码是否可以编译...请尝试

using System;

namespace Ticket
{
    class blah
    {
        public void abc()
        {

            int numberOfTickets;
            int numberOfAvailableTickets=10;
            int cost = 100;
            int pay;
            Console.WriteLine("how many tickets do you need");
            numberOfTickets = Convert.ToInt32(Console.ReadLine());

            try
            {

            if( numberOfTickets>numberOfAvailableTickets)
                throw new Exception ("Not enough Tickets available!");


                    pay = 100 * numberOfTickets;
                    Console.WriteLine("Pay please");
                    Console.WriteLine(pay);

            }
            catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
        }
    }

}
    class Theater
    {
        static void Main(string[] args)
        {
            blah hi = new blah();
            hi.abc();
            Console.ReadLine();

        }
    }
}


如果输入的号码超出可用票证,则为throw

08-05 22:54