2017-06-09 117 views
-4

我想知道是否有可能在运行程序时执行特定的代码段(程序是命令行界面)。C#执行特定代码

程序按顺序执行所有操作,但这不是我想要的。

下面是两个程序运行的截图以及它背后的代码。

[Program while running]

 // Read all the bookings between dates: 
     Console.WriteLine("Input start date in the format yyyy-mm-dd"); 
     DateTime bookingStartDate = DateTime.Parse(Console.ReadLine()); 
     Console.WriteLine("Input end date in the format yyyy-mm-dd"); 
     DateTime bookingEndDate = DateTime.Parse(Console.ReadLine()); 
     DbBookings bookings = new DbBookings(bookingStartDate, bookingEndDate); 
     foreach (DbBooking booking in bookings.Bookings) 
     { 
      Console.WriteLine("booking start date = " + booking.startDate); 
      Console.WriteLine("booking end date = " + booking.endDate); 
      Console.WriteLine("booking room No = " + booking.roomNo); 
      Console.WriteLine("booking guest = " + booking.guestName); 
      Console.WriteLine("booking guest phone No = " + booking.guestPhone); 
      Console.WriteLine("booking cost = " + booking.cost); 
     } 


     //Create a new booking 
     Console.WriteLine("Bed and breakfast create new booking"); 

     Console.WriteLine("Input start date in the format yyyy-mm-dd"); 
     DateTime startDate = DateTime.Parse(Console.ReadLine()); 
     Console.WriteLine("Input end date in the format yyyy-mm-dd"); 
     DateTime endDate = DateTime.Parse(Console.ReadLine()); 
     Console.WriteLine("What room?"); 
     int room = int.Parse(Console.ReadLine()); 
     Console.WriteLine("Customer Name"); 
     string name = Console.ReadLine(); 
     Console.WriteLine("Customer phone number"); 
     string phone = Console.ReadLine(); 
     double cost = (endDate.ToOADate() - startDate.ToOADate()) * 29; 

     DbBooking newBooking = new DbBooking(
      startDate, 
      startDate, 
      room, 
      name, 
      phone, 
      cost 
      ); 
     Console.ReadLine(); 

我想要什么程序做的是要问两个问题,“你想阅读的预约”和“你要添加新的预订”你会回答这些通过输入'1'或'2'来输入问题

+0

[这个答案](https://stackoverflow.com/a/11141262/5174469)应该给你如何创建这样的接口 –

+0

测试参数计算正确的开始。如果为零,则从ReadLine()获取输入。如果大于零,则从args数组中获取值。使用:args.Count() – jdweng

+0

不要发布代码的图片,将代码复制到问题中,然后突出显示该代码,并在代码在预览窗口中显示格式不正确的情况下按下“{}”工具栏按钮。 –

回答

0

如果您希望基于不同用户输入值的行为不同,则需要在代码中引入逻辑。 This link将解释if,else ifelse关键字是如何工作的。另外,如果你想扩大你对分支的了解,你可以使用switch statements

static void Main(string[] args) { 
    Console.WriteLine("Do you want to read the bookings?"); 
    string response = Console.ReadLine(); 
    if (response == "1") 
     ReadBookings(); 
    else { 
     Console.WriteLine("Do you want to create a booking?"); 
     response = Console.ReadLine(); 
     if (response == "1") 
      CreateBooking(); 
    } 

    Console.WriteLine("Press any key to exit."); 
    Console.ReadKey(); 
} 

static void ReadBookings() { 
    DateTime startDate, endDate; 

    Console.WriteLine("Input start date in the format yyyy-mm-dd"); 
    startDate = DateTime.Parse(Console.ReadLine()); 

    Console.WriteLine("Input end date in the format yyyy-mm-dd"); 
    endDate = DateTime.Parse(Console.ReadLine()); 

    DbBookings bookings = new DbBookings(bookingStartDate, bookingEndDate); 
    foreach (DbBooking booking in bookings.Bookings) 
     Console.WriteLine($"booking start date: {booking.startDate}\nbooking end date: {booking.endDate}\nbooking room No: {booking.roomNo}\nbooking guest: {booking.guestName}\nbooking guest phone No: {booking.guestPhone}\nbooking cost: {booking.cost}); 
} 

static void CreateBooking() { 
    DateTime startDate, endDate; 
    string name, phone; 
    int room; 
    double cost; 

    Console.WriteLine("Bed and breakfast create new booking"); 
    Console.WriteLine("Input start date in the format yyyy-mm-dd"); 
    startDate = DateTime.Parse(Console.ReadLine()); 

    Console.WriteLine("Input end date in the format yyyy-mm-dd"); 
    endDate = DateTime.Parse(Console.ReadLine()); 

    Console.WriteLine("What room?"); 
    room = int.Parse(Console.ReadLine()); 

    Console.WriteLine("Customer Name"); 
    name = Console.ReadLine(); 

    Console.WriteLine("Customer phone number"); 
    phone = Console.ReadLine(); 
    cost = (endDate.ToOADate() - startDate.ToOADate()) * 29; 

    DbBooking newBooking = new DbBooking(startDate, endDate, room, name, phone, cost); 
} 
+0

另请注意,Console.WriteLine像string.Format一样处理替换,并且C#6.0具有行为相同但具有更好可读性的'$'字符。你可以像'@'逐字使用它。 – lxxtacoxxl