我的程序似乎没有遵循我的If语句

时间:2016-10-11 02:25:06

标签: c#

        //Declarations 
        double height;
        double weight;
        double BMI;
        int Const;


        //Reading User Input

        //HEIGHT
            Console.WriteLine("Please enter the person's height in inches: ");
            height = Convert.ToDouble(Console.ReadLine());

                if (height < 5 && height > 120)
                {
                    Console.WriteLine("The height entered must be between 5” and 120” inclusive.");

        }
            //MASS
            Console.WriteLine("Please enter the person's weight in lbs: ");
            weight = Convert.ToDouble(Console.ReadLine());
                if (weight < 0.5 && weight > 999)
                {
                     Console.WriteLine("The weight entered must be between 0.5 lb. and 999 lb. inclusive.");
                }

            //BMI Calculations
            Const = 703;
            BMI = (weight / (height * height)) * Const;


            //Category Assignments
            if (BMI <= 16)
            {
                Console.WriteLine("The BMI for a " + height + "tall person who weighs " + weight + " lb. is 26.7, which is categorized as 'serverly underwieght'.");
            }
            else if (BMI > 16 && BMI <= 18.5)
            {
                Console.WriteLine("The BMI for a " + height + "tall person who weighs " + weight + " lb. is 26.7, which is categorized as 'underwieght'.");
            }
            else if (BMI > 18.5 && BMI <= 25)
            {
                Console.WriteLine("The BMI for a " + height + "tall person who weighs " + weight + " lb. is 26.7, which is categorized as 'healthy'.");
            }
            else if (BMI > 25 && BMI < -30)
            {
                Console.WriteLine("The BMI for a " + height + "tall person who weighs " + weight + " lb. is 26.7, which is categorized as 'Overweight'.");
            }
            else if (BMI > 30)
            {
                Console.WriteLine("The BMI for a " + height + "tall person who weighs " + weight + " lb. is 26.7, which is categorized as 'Obese'.");
            }



        }

    }
}

这里的第一个问题,很遗憾没有制作正确的格式。无论如何,我的程序在输入重量后立即关闭,就像瞬间一样。它是一个控制台应用程序btw。

另外,如果我输入的重量或高度低于或高于要求,它不会显示错误信息,只是继续然后关闭。

3 个答案:

答案 0 :(得分:1)

如果您在范围5和120之间进行检查,则应如下所示,因为height < 5 && height > 120将返回false。

if (height > 5 && height < 120)
  {
   Console.WriteLine("The height entered must be between 5” and 120” inclusive.");
  }

同样对于体重,

if (weight > 0.5 && weight < 999)
 {
  Console.WriteLine("The weight entered must be between 0.5 lb. and 999 lb. inclusive.");
 }

如果你想在控制台中看到输出,在程序的最后添加它

Console.ReadLine()

将等到用户按某个键

答案 1 :(得分:0)

在程序结束时添加Console.ReadLine,它将保持打开状态。

答案 2 :(得分:0)

最后添加一个额外的Console.ReadLine或Console.ReadKey。这将强制控制台应用程序在退出之前等待用户键入enter或任何其他键。