简体   繁体   English

C#控制台计算器有关if语句

[英]C# Console Calculator about if statement

I have no idea know what's wrong with my if statement. 我不知道我的if语句怎么了。 The program just switch off after Asked the user to enter their gender. 要求用户输入性别后,程序才关闭。 Enter 1 If you are Male Enter 2 If you are Female 输入1如果您是男性输入2如果您是女性

        static void Main(string[] args) {

        int WaistToHeightCalculatorOption,GenderOption;

        double HeightCm = 0.0, WaistCm = 0.0; 
        double WaistToHeightRatio = 0.0;
        string WaistToHeightCalculatorMenu = ("Which Measurement You Want to use to enter the weight and height?"
                                           + "\n1)Enter 1 for Metric"
                                           + "\n2)Enter 2 for British Imperial:");
        Console.Write(WaistToHeightCalculatorMenu);
        WaistToHeightCalculatorOption = int.Parse(Console.ReadLine());

        if (WaistToHeightCalculatorOption == 1) {
            Console.Write("\nPlease Enter your Height in cm:");
            HeightCm = double.Parse(Console.ReadLine());
            Console.Write("\nPlease Enter your Waist in centimetres (cm):");
            WaistCm = double.Parse(Console.ReadLine());
            WaistToHeightRatio = WaistCm / HeightCm;
            Console.WriteLine("Your Waist to Height Ratio is {0}", WaistToHeightRatio);

            Console.Write("\n1)Enter 1 If you are Male"
                        + "\n2)Enter 2 If you are Female:");
            GenderOption = int.Parse(Console.ReadLine());

            if (GenderOption ==1) {
                if (WaistToHeightRatio >= 0.536) {
                    Console.WriteLine("Your Risk of Obesity Related Cardiovascular Diseases is at High Risk");
                } else if (WaistToHeightRatio < 0.536) {
                    Console.WriteLine("Your Risk of Obesity Related Cardiovascular Diseases is at low Risk");
                }
            } else if (GenderOption == 2) {
                if (WaistToHeightRatio >= 0.492) {
                    Console.Write("Your Risk of Obesity Related Cardiovascular Diseases is at High Risk");
                } else if (WaistToHeightRatio < 0.492) {
                    Console.Write("Your Risk of Obesity Related Cardiovascular Diseases is at low Risk");
                }
            }
        }

You can just press Ctr + F5 in Visual Studio or add Console.ReadKey(); 您可以在Visual Studio中按Ctr + F5或添加Console.ReadKey(); at the end to wait for user to enter any key before exit the program. 最后等待用户输入任何键,然后退出程序。

Because after the last input , you just print to screen and exit the program. 因为在最后一次输入之后,您只需要打印到屏幕并退出程序即可。

Also your indentation can be confusing, I think this way is more readable. 另外,您的缩进可能会造成混淆,我认为这种方式更具可读性。

what happens if gender is not 1 or 2 ? 如果性别不是1或2,会发生什么? you should let the user know that he make a mistake , or loop till you get what you want (1 or 2) 您应该让用户知道他犯了一个错误,或者循环直到您得到所需的内容(1或2)

        if (GenderOption ==1){
            if (WaistToHeightRatio >= 0.536) {
                Console.WriteLine("Your Risk of Obesity Related Cardiovascular Diseases is at High Risk");
            } 
            else {
                Console.WriteLine("Your Risk of Obesity Related Cardiovascular Diseases is at low Risk");
            }
        } 
        else 
            if (GenderOption == 2) {
                if (WaistToHeightRatio >= 0.492) {
                    Console.Write("Your Risk of Obesity Related Cardiovascular Diseases is at High Risk");
                } 
                else {
                    Console.Write("Your Risk of Obesity Related Cardiovascular Diseases is at low Risk");
                }
            }

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

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