我想知道在运行程序时是否有可能由用户执行特定的代码(程序是命令行界面)。
程序按顺序执行所有操作,但这不是我想要的。
以下是程序运行方式及其背后代码的两个屏幕截图。
[]
// 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();
我希望程序要做的是提出2个问题,“你想阅读预订吗?”和“你想要添加新预订”,你可以通过输入'1'或'2'来回答问题
答案 0 :(得分:0)
如果您希望基于不同的用户输入值使其行为不同,则需要在代码中引入逻辑。 This link将解释if
,else if
和else
关键字的工作原理。此外,如果您想进一步扩展分支知识,可以使用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);
}