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

时间:2014-03-25 05:45:47

标签: c# console-application calculator

我不知道我的if语句有什么问题。 该程序在询问用户输入其性别后才关闭。 输入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");
                }
            }
        }

2 个答案:

答案 0 :(得分:2)

您可以在Visual Studio中按Ctrl + F5或在末尾添加Console.ReadKey();以等待用户在退出程序之前输入任何键。

答案 1 :(得分:1)

因为在最后一次输入后,您只需打印到屏幕并退出程序。

你的缩进也可能令人困惑,我认为这种方式更具可读性。

如果性别不是1或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");
                }
            }