BMI计算和菜单逻辑

BMI calculation and Menu logic

我对编程完全陌生,一直在绞尽脑汁思考一些我知道非常基础的事情。我不是一个在线学习的人,所以这被证明是更困难的。

所以我需要创建一个 C# 控制台应用程序,它有一个菜单,其中有三个选项,其中包括一个 'exit'。到目前为止,我只能 运行 分别选择选项 1 和 2。当我查看会员费率时,我只能选择选项 1。然后,当我尝试将会员和 BMI 计算的编码放在一起时,我仍然只能 运行 会员!

下面是我的代码:

using System;

namespace BMITEST1MILLION
{
    class Program
    {
        static void Main(string[] args)

        {
            Console.WriteLine("Kiaora! Welcome to City Gym. Please select one of the following options:");
            Console.WriteLine("1.) View Our Membership Rates");
            Console.WriteLine("2.) Calculate My BMI");
            Console.WriteLine("3.) Exit");

            int num = Int32.Parse(Console.ReadLine());

            if (num == 1) ////condition.run if user main menu input is option 1 
            {
                Console.Clear();//clears previous screen

                Console.WriteLine("Which membership type would you like to view?"); //prompting user input
                Console.WriteLine("1.) Basic");
                Console.WriteLine("2.) Regular");
                Console.WriteLine("3.) Premium");


                int num1 = Int32.Parse(Console.ReadLine()); //declare if option 1 chosen
                if (num1 == 1) //condition. run if user input for membership type is 1
                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Basic Membership Rate is  per week or  per month");
                }


                int num2 = Int32.Parse(Console.ReadLine()); //declare if option 2 chosen
                if (num2 == 2) ; //condition. run if user input for membership type is 2
                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Regular Membership Rate is  per week or  per month");
                }

                int num3 = Int32.Parse(Console.ReadLine()); //declare if option 3 chosen
                if (num3 == 3) //condition. run if user input for membership type is 3

                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Premium Membership Rate is  per week or  per month");
                }


                if (num == 2)//condition.run if user main menu input is option 2 (BMI Calcuation)

                    //BMI calculation

                    Console.Write("Enter you height in metres (m):"); //ask user to input their height in metres
                double userHeight = Convert.ToDouble(Console.ReadLine()); // convert string to a double

                Console.Write("Enter you weight in kilograms (kg):"); //ask user to input their wedight in kilograms
                double userWeight = Convert.ToDouble(Console.ReadLine()); // convert string to a double

                double BMI = userWeight / (userHeight * userHeight); //method to calculate BMI based on user input weight and height
                Console.WriteLine("Your BMI is " + Math.Round(BMI, 2)); //print BMI result to screen. Round result to two decimals.

                if (BMI <= 18.5)//condition
                    Console.WriteLine("You are underweight.");

                else
                    if (BMI <= 25)//condition
                    Console.WriteLine("Your weight is normal.");

                else
                    if (BMI <= 30)//condition
                    Console.WriteLine("You are overweight.");

                else //if BMI is greater then 30
                    Console.WriteLine("You are obese.");

                Console.ReadKey();

            }//end of bmi calcuation

        }//main menu
    }//end of class
}//end of namespace

我推荐使用 do 和 switch-case:

do {
  switch(option) {
    case 1: //do something
    break;
 
    case 2: //do something
    break;

    case 3: //exit
    break;

    default: // wrong option
    break;
  }
}while(option != 3);

此致!

您的 main if 语句的结构似乎有点不对劲。为了执行选项 1 或选项 2,它将是

if(num == 1){
  ...
}
if(num == 2){
  ...
}

但你有

if(num == 1){
  ...
  if(num == 2)
}

也许这就是您将代码放在一起的意思?在这种情况下,由于 if(num == 2) 后面没有大括号,它只会应用到它下面的行,即打印“Enter you height in meters (m):”的行。但是,由于它只检查 num 是否已经等于 1 的条件(因为它嵌套在 num == 1 块中),所以该行永远不会执行,因此它跳过该行并继续 double userHeight = Convert.ToDouble(Console.ReadLine());。希望这对您有所帮助!