几个菜单选项返回不正确

A couple menu choices coming back incorrect

整个程序的草稿,但我正在努力让我的菜单正常工作,运行 遇到了一个我无法解决的问题。在测试客户菜单的应用程序时,1-4 return 正确但 5 不正确。我想要 5 只是 return 到主菜单,但它是 return 我的错误检查输出。同样的问题也出现在管理器菜单中,其中 1-4 return 正确但 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 将始终被视为无效。

我还想给您一些提示,如果您愿意,可以使您的代码更短更简单。

首先,您可以让代码始终进入循环,并且 return 只有在输入有效时才进入循环。这样你D就不会R重复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: ");
    } 
}

其次,我建议您看一下 switch-case 声明。

您可以像这样在代码的多个地方使用它:

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()。 它将 return 无需用户按回车键,一次只需要一个键。 我个人认为它非常适合这类菜单。