如何允许用户随时通过输入 C# 退出控制台

How to allow a user to exit from the Console at any time C# with an input

我对 C# 编程还很陌生,所以请多多关照。 我目前正在尝试在基于菜单的控制台中为 BMI 计算器制作一段代码,允许用户从不同的选项中 select 并给出不同的结果。

我还没有完成我的代码,但在我到达被告知的地步之前,一切都相对顺利 - “当程序处于 运行 时,还应该有一个‘退出’或终止的选项。”

我查看了多个论坛并试图找到答案,none 我已经能够使用我的代码从中找到答案。 我希望这里有人能够帮助我并指出正确的方向。

这是我目前正在使用的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BMI_Application
{
    class Program
    {
        static void Main(string[] args)
        {
            bool showMenu = true;
            while (showMenu)
            {
                showMenu = MainMenu();
            }

        }

        private static void Calculator()
        {

        }
        private static bool MainMenu()
        {

            string bmi = "You have selected the BMI Calculator";
            float height;
            float weight;
            float sum1; // Heightx2
            float sum2; // Weight / sum1
            
            Console.WriteLine("Please select an option");
                Console.WriteLine("1: BMI Calculator");
                Console.WriteLine("2: Membership Rates");
                Console.WriteLine("3: Close Menu");
                Console.WriteLine("\r\nPlease enter 1, 2 or 3");
                Console.WriteLine("\r\nYou can also write 'bye' at any time to leave");

            switch (Console.ReadLine())
            {
                case "1":
                    Console.Clear();
                        Console.WriteLine(bmi);
                        Console.WriteLine("Please enter your Height in Metres");
                        height = float.Parse(Console.ReadLine());
                        Console.WriteLine("Please enter your Weight in Kilograms");
                        weight = float.Parse(Console.ReadLine());
                        sum1 = height * height;
                        sum2 = weight / sum1;
                        Console.WriteLine("Your BMI is : {0}", sum2); // Next part is - based on their BMI Level, writing if they are underweight etc
                    if (sum2 <= 18.5)
                    {
                        Console.WriteLine("Your BMI level indicates that your are 'Underweight' "); // Underweight
                    }
                    if (sum2 >= 18.5 && sum2 <= 25)
                    {
                        Console.WriteLine("Your BMI level indicates that you are 'Normal' weight"); // Normal
                    }
                    if (sum2 >= 25 && sum2 <= 30)
                    {
                        Console.WriteLine("Your BMI level indicates that you are 'Overweight' ");// Overweight
                    }
                    if (sum2 >= 30)
                    {
                        Console.WriteLine("Your BMI level indicates that you are 'Obese' "); // Obese
                    }
                        Console.WriteLine("\r\nPress any key to return back to main menu");
                        Console.ReadKey();
                        Console.Clear();
                        return true;

                    case "2":
                        Console.Clear();
                        Console.WriteLine("You have selected the Membership Rates");
                        Console.WriteLine("What Membership would you like?");
                        return true;
                    case "3":
                        Console.WriteLine("You have selected to close the menu, press any key to continue"); // Terminate
                        Console.ReadKey();
                        return false;
                    case "bye":
                        Console.WriteLine("Goodbye! Push any key to get out of here!");
                        Console.ReadKey();
                        return false;

                    default:
                        Console.Clear();
                        Console.WriteLine("That is not a valid number, please enter 1, 2 or 3"); // Not valid entry
                        Console.WriteLine(" ");
                        return true;
                }
            }
        }
            
    }

关于如何让用户通过输入单词 'exit' 退出控制台的任何想法?

非常感谢

因此,如果您希望允许用户在任何阶段输入诸如“exit”之类的文本来退出程序,那么在您读取输入的每个点都需要支持此功能。

您的代码目前的结构方式很难做到,因为您的代码中基本上只有 1 个方法可以完成所有工作。而你实际上是在把你的“计算器”和你的“菜单”混在一起,它们应该分开。

如果您查看您的代码,那么您实际上会发现您多次重复代码部分,只是略有不同(写入和读取 to/from 控制台)。 相反,您需要构建代码以减少代码重复,而是将其拆分为单独的方法。

这实际上是你程序的症结所在。由于您的代码的结构方式,您无法轻松调整它以支持新需求而不重新编写它。

现在我不打算为您重写您的代码,但我会提示您使用一种方法来部分完成此操作(还有其他方法!) 尝试将写作和阅读分开成一个单独的方法。然后你也可以在一个单独的方法中检查“退出”这个词。

所以伪代码应该是这样的;

//do something in your method
var inputText = WriteAndGetInput("Please select option 1,2,3\r\nBlah blah blah \r\n");
CheckForExit(inputText);

//If you reach here, then Exit was not entered so carry on with your program and check for your real input.
//Try to use methods as to not repeat code.


//Method to write to console and get input
private static string WriteAndGetInput(string textToOutput)
{
       Console.WriteLine(textToOutput);
       return Console.ReadLine();  
}

//method to check if user typed exit
private static void CheckForExit(string inputText)
{
    if (inputText.Equals("exit"))
    {
        Console.WriteLine("You chose to exit.   Goodbye!");
        System.Environment.Exit(0); 
    }
}