首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BMI计算与菜单逻辑

BMI计算与菜单逻辑
EN

Stack Overflow用户
提问于 2022-04-11 12:18:24
回答 2查看 297关注 0票数 -1

我对编程是完全陌生的,我一直在绞尽脑汁地想着一些我知道的非常基本的东西。我不是一个在网上学习的人,所以事实证明这更加困难。

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

下面是我的代码:

代码语言:javascript
复制
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 $10 per week or $40 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 $15 per week or $60 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 $20 per week or $80 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-11 12:32:19

我建议使用do和switch-case:

代码语言:javascript
复制
do {
  switch(option) {
    case 1: //do something
    break;
 
    case 2: //do something
    break;

    case 3: //exit
    break;

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

致以问候!

票数 0
EN

Stack Overflow用户

发布于 2022-04-11 12:37:52

似乎您的主要if语句的结构有点偏离。为了执行备选方案1或备选方案2,它将如下

代码语言:javascript
复制
if(num == 1){
  ...
}
if(num == 2){
  ...
}

但你有

代码语言:javascript
复制
if(num == 1){
  ...
  if(num == 2)
}

也许这就是你把代码放在一起的意思?在这种情况下,由于在if(num == 2)之后没有大括号,所以它只适用于它下面的直线(即打印“输入高度(米):”)。但是,由于它只在num已经等于1(因为它嵌套在num == 1块中)时才检查此条件,所以该行将永远不会执行,因此它跳过该行并继续执行double userHeight = Convert.ToDouble(Console.ReadLine());。希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71827720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档