几个菜单选项返回不正确

整个程序的草稿,但是我试图使菜单正常工作,并且遇到了无法解决的问题。测试客户菜单的应用程序时,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,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);
    }
}
}
jackVSjacky 回答:几个菜单选项返回不正确

while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5")

如果您查看此行,您会发现括号中不包含“ 5”。 因此,您正在检查答案是否是1-4以外的值,或者是5。 含义5将始终视为无效。

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

首先,您可以使代码始终进入循环并仅在输入有效后才返回。这样,您 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()。 它会返回,而无需用户按下Enter键,一次只需要按下一个键。 就我个人而言,我认为这对于此类菜单是完美的。

本文链接:https://www.f2er.com/3120361.html

大家都在问