有几个菜单选择不正确



整个程序的初稿,但我正试图让菜单正常工作,遇到了一个我无法解决的问题。在测试客户菜单的应用程序时,1-4正确返回,但5不正确。我希望5只返回到主菜单,但它正在返回我的错误检查输出。同样的问题也出现在管理器菜单中,其中1-4正确返回,但5和6不正确。它们还将我引导到错误检查输出。如何修改代码以使每个子菜单功能都能正常工作?

MainMenu();
Console.ReadKey();
}
static void MainMenu()
{
int userChoice = MainMenuChoice(); // Reading in the userChoice with the MenuChoice method
if (userChoice == 3) // if user enters 3, the program ends
{
Console.WriteLine("Thank you, Goodbye!");
}
while (userChoice != 3)
{
if (userChoice == 1)
{
Console.WriteLine("Welcome to the customer menu!n"); // The customer menu is brought up if user enters 1
CustomerMenu();
}
if (userChoice == 2)
{
Console.WriteLine("Welcome to the manager menu!n"); // The manager menu is brought up if user enters 2
ManagerMenu();
}
userChoice = MainMenuChoice(); // program ends
if (userChoice == 3)
{
Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");
}
}
}
static int MainMenuChoice()
{
Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
Console.WriteLine("Welcome to VideoMart at University Boulevard!  nAt VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!");
Console.WriteLine("nPress 1 if you are a customer");
Console.WriteLine("nPress 2 if you are a manager");
Console.WriteLine("nPress 3 to Exit");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
string choice = Console.ReadLine();
Console.WriteLine();
while (!(choice == "1" || choice == "2" || choice == "3")) // error checking
{
Console.WriteLine("Please try again");
Console.WriteLine("Press 1 if you are a customer");
Console.WriteLine("Press 2 if you are a manager");
Console.WriteLine("Press 3 to Exit");
choice = Console.ReadLine();
}
return int.Parse(choice);
}

static void CustomerMenu() {
int customerChoice = CustomerMenuChoice(); // Reading in the customerChoice into the CustomerMenuChoice method
if (customerChoice == 5) // if user enters 5, the user is returned to the main menu
{
Console.WriteLine("Returning to main menu, thank you.");
}
while (customerChoice != 5)
{
if (customerChoice == 1)
{
Console.WriteLine("This shows movies available.n"); // this option gives the user the opportunity to view all movies available to rent
//MoviesAvailable();
}
if (customerChoice == 2)
{
Console.WriteLine("This is to rent a movie.n"); // this option gives the user the opportunity to rent a movie, with email address
//RentMovie();
}
if (customerChoice == 3)
{
Console.WriteLine("This is to show my rented movies.n"); // this option gives the user the opportunity to view movies a user currently has rented, with email address
//MyRentedMovie();
}
if (customerChoice == 4)
{
Console.WriteLine("This is to return a movie.n"); // this option gives the user the opportunity to return a movie rented
//ReturnMovie();
}
customerChoice = CustomerMenuChoice();
if (customerChoice == 5)
{
Console.WriteLine("Returning to main menu, thank you.");
}
}            
}
static int CustomerMenuChoice()
{
Console.WriteLine("Below is a list of actions that can be performed by customers: ");
Console.WriteLine("nPress 1 to view movies available to rent.");
Console.WriteLine("nPress 2 to rent a movie.");
Console.WriteLine("nPress 3 to view a list of movies you currently have rented.");
Console.WriteLine("nPress 4 to return a movie rented.");
Console.WriteLine("nPress 5 to return to menu.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
string customerChoice2 = Console.ReadLine();
Console.WriteLine();
while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5") // error checking
{
Console.WriteLine("Please enter a valid option: ");
Console.WriteLine("nPress 1 to view movies available to rent.");
Console.WriteLine("nPress 2 to rent a movie.");
Console.WriteLine("nPress 3 to view a list of movies you currently have rented.");
Console.WriteLine("nPress 4 to return a movie rented.");
Console.WriteLine("nPress 5 to return to menu.");
customerChoice2 = Console.ReadLine();
}
return int.Parse(customerChoice2);
}
static void ManagerMenu() {
int managerChoice = ManagerMenuChoice(); // Reading in the managerChoice into the ManagerMenuChoice method
if (managerChoice == 6) // if user enters 6, the user is returned to the main menu
{
Console.WriteLine("Returning to main menu, thank you.");
}
while (managerChoice != 6)
{
if (managerChoice == 1)
{
Console.WriteLine("This is to add a movie to inventory.n"); // this option gives the manager the ability to add a movie to inventory
//AddMovie();
}
if (managerChoice == 2)
{
Console.WriteLine("This is to remove a movie from inventory.n"); // this option gives the manager the ability to remove a movie from inventory
//RemoveMovie();
}
if (managerChoice == 3)
{
Console.WriteLine("This is to edit a movie from inventory.n"); // this option gives the manager the ability to edit a movie to inventory
//EditMovie();
}
if (managerChoice == 4)
{
Console.WriteLine("This is to process a btach transaction file.n"); // this option gives the manager the ability to process a batch transaction file
//BatchTransaction();
}
if (managerChoice == 5)
{
Console.WriteLine("This is to access the report menu.n"); // this option gives the manager the ability to access the report menu
//ReportMenu();
}
managerChoice = ManagerMenuChoice();
if (managerChoice == 6)
{
Console.WriteLine("Returing to main menu, thank you.!"); // returns to main menu
}
}            
}
static int ManagerMenuChoice()
{
Console.WriteLine("Below is a list of actions that can be performed by managers: ");
Console.WriteLine("nPress 1 to add a new movie to to the inventory.");
Console.WriteLine("nPress 2 remove a movie from inventory.");
Console.WriteLine("nPress 3 to edit a movie in inventory.");
Console.WriteLine("nPress 4 to process a btach transaction file.");
Console.WriteLine("nPress 5 to access the report menu.");
Console.WriteLine("nPress 6 to return to the menu.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
string managerChoice2 = Console.ReadLine();
Console.WriteLine();
while (!(managerChoice2 == "1" || managerChoice2 == "2" || managerChoice2 == "3" || managerChoice2 == "4") || managerChoice2 == "5" || managerChoice2 == "6") // error checking
{
Console.WriteLine("Please enter a valid option: ");
Console.WriteLine("nPress 1 to add a new movie to to the inventory.");
Console.WriteLine("nPress 2 remove a movie from inventory.");
Console.WriteLine("nPress 3 to edit a movie in inventory.");
Console.WriteLine("nPress 4 to process a btach transaction file.");
Console.WriteLine("nPress 5 to access the report menu.");
Console.WriteLine("nPress 6 to return to the menu.");
managerChoice2 = Console.ReadLine();
}
return int.Parse(managerChoice2);
}
}
}
while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5")

如果你看这一行,你可以看到括号中没有"5"。所以你在检查答案是不是1-4或者是5。意味着5将始终被视为无效。

如果你愿意的话,我也想给你一些技巧,让你的代码变得更短更简单。

首先,您可以使代码始终进入循环,并且只有在输入有效时才返回。这样你就可以DR自己Y(DRY

static int CustomerMenuChoice()
{
Console.WriteLine("Below is a list of actions that can be performed by customers: ");
while(true)
{
Console.WriteLine("nPress 1 to view movies available to rent.");
Console.WriteLine("nPress 2 to rent a movie.");
Console.WriteLine("nPress 3 to view a list of movies you currently have rented.");
Console.WriteLine("nPress 4 to return a movie rented.");
Console.WriteLine("nPress 5 to return to menu.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
string customerChoice = Console.ReadLine();
if (customerChoice == "1" || customerChoice == "2" || customerChoice == "3" || customerChoice == "4" || customerChoice == "5")
{
return int.Parse(customerChoice);
}
Console.WriteLine("Please enter a valid option: ");
} 
}

其次,我建议看一下switchcase语句。

你可以在代码的几个地方使用它,比如:

static void CustomerMenu()
{
int customerChoice = 0;
while (customerChoice != 5)
{
customerChoice = CustomerMenuChoice();
switch (customerChoice)
{
case 1:
Console.WriteLine("This shows movies available.n");
break;
case 2:
Console.WriteLine("This is to rent a movie.n");
break;
case 3:
Console.WriteLine("This is to show my rented movies.n");
break;
case 4:
Console.WriteLine("This is to show my rented movies.n");
break;
case 5:
Console.WriteLine("Returning to main menu, thank you.");
break;
default:
// Option not in the list
break;
}
}
}

最后一件事,您可能想研究使用Console.ReadKey((而不是Console.ReadLine((。它将在用户无需按下回车键的情况下返回,并且一次只需按一个键。就我个人而言,我认为它非常适合这类菜单。

相关内容

  • 没有找到相关文章

最新更新