简体   繁体   English

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

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

I am very new to programming in C# so bare with me.我对用 C# 编程很陌生,所以我很裸露。 I am currently trying to make a block of code for a BMI Calculator within a menu based Console that allows the user to select from different options and it gives them different results.我目前正在尝试在基于菜单的控制台中为 BMI 计算器制作一个代码块,允许用户从不同的选项中进行选择,并为他们提供不同的结果。

I have not completed my code yet but everything has been relatively smooth sailing until I got to the point where I have been told - "There should also be an option to 'exit' or terminate at any stage when the program is running."我还没有完成我的代码,但一切都相对顺利,直到我被告知 - “当程序运行时,还应该有一个在任何阶段‘退出’或终止的选项。”

I have looked on multiple forums and have tried to find an answer, none of which I have been able to get to work with my code.我查看了多个论坛并试图找到答案,但我都无法使用我的代码。 I was hoping someone here would be able to help me out and point me in the right direction.我希望这里有人能够帮助我并指出我正确的方向。

Here is the code I am currently working with.这是我目前正在使用的代码。

 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; } } } }

Any ideas of how I would be able to get a user to exit out of the console by entering the word 'exit' for example?例如,关于如何让用户通过输入“退出”一词退出控制台的任何想法?

Thanks very much非常感谢

So if you want to allow the user to enter a text such as "exit" to quit the program at any stage, then at every point where you read input you need to support this.因此,如果您想允许用户在任何阶段输入诸如“退出”之类的文本以退出程序,那么在您读取输入的每个点都需要支持这一点。

The way your code is structured at the moment then this is difficult as you basically have 1 method in your code which is doing everything.您的代码目前的结构方式很困难,因为您的代码中基本上有 1 个方法可以完成所有工作。 And you are actually mixing your "calculator" together with your "menu", which should be separated.你实际上是将你的“计算器”和你的“菜单”混合在一起,这应该是分开的。

And if you look at your code then you will actually see that you are repeating sections of code multiple times with just slight differences (Writing and reading to/from console).如果您查看您的代码,那么您实际上会发现您正在多次重复代码部分,只是略有不同(向/从控制台写入和读取)。 Instead, you need to structure your code so that you have less repeat of code and instead split this out into separate methods.相反,您需要构建代码以减少代码重复,而是将其拆分为单独的方法。

This is actually the crux of your program.这实际上是您程序的关键。 Because the way your code is structured, you can't easily adapt it to support the new requirement without re-writing it.因为您的代码的结构方式,如果不重新编写它,就不能轻易地调整它以支持新的需求。

Now I'm not going to re-write your code for you, but I will give you hints on one way to partly do this (there are other ways!) Try and separate out the writing & reading into a separate method.现在我不会为你重新编写你的代码,但我会给你一些提示来部分做到这一点(还有其他方法!)尝试将编写和阅读分开到一个单独的方法中。 Then you can also check for the word "exit" in a separate method.然后,您还可以在单​​独的方法中检查“退出”一词。

So Pseudo code would be like this;所以伪代码是这样的;

//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); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM