C#引用参数代码运行示例

关于引用参数

本文主要贴上一段关于C#中引用参数的调试代码,示例主要内容是计算BMI指数;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;//此处声明用于数学类方法调用

///涉及的主要知识点在于引用参数、
///C#数据的输入读取、类型转换和小数点精确

namespace Chapter5_Reference
{
    class myClass
    {
        public int weight;
        public double height;
        public double BMI;
        
        public double myFun()//定义一个方法,功能主要是读取身高和体重等参数,再者计算BMI并返回
        {

            Console.WriteLine("The BMI is defined as the body mass divided by the square of the body height,");
            Console.WriteLine("Please input your height(cm):");
            height = Convert.ToDouble(Console.ReadLine());//读取身高单位cm
            Console.WriteLine("Please input your weight(kg):");
            weight = Convert.ToInt32(Console.ReadLine());//读取体重单位kg
            BMI = weight / (height * height);//计算BMI
            //Console.WriteLine("so your BMI value is :"+BMI);
            return BMI;
        }
        

    }
    class Program
    {
        static void myMethod(ref myClass mc, ref int age)
        {
            mc.BMI *= 10000;//BMI数据还原
            mc.BMI=Math.Round(mc.BMI, 3);//将BMI指数精确到小数点后三位
            //结果输出
            Console.WriteLine("The Result as follow:");
            if (mc.BMI < 18.5)
               Console.WriteLine("BMI : "+mc.BMI+" , which is less than 18.5, maybe you are underweight.");
            else if(mc.BMI>=18.5&&mc.BMI<=25)
               Console.WriteLine("BMI : " + mc.BMI + " , which is normal, Congratulation!");
            else
               Console.WriteLine("BMI : "+ mc.BMI + " , which is greater than 25, maybe you are overweight.");
            age += 1;
               Console.WriteLine("AGE : {0}",age);
        }
        static void Main(string[] args)
        {
            myClass MM = new myClass();//入口
            MM.myFun();//类中方法调用
            Console.WriteLine("Please input your age: ");
            int Age = Convert.ToInt32(Console.ReadLine());
            myMethod(ref MM, ref Age);//方法调用,引用参数;调用结束之后原形参mc和age失效
            Console.ReadKey();
        }
    }
}

运行结果:

C#引用参数代码运行示例_第1张图片

你可能感兴趣的:(C#,c#,调试,class,引用参数)