首页 > 解决方案 > 如何从另一个类c#中调用一个类

问题描述

我正在使用 4 种方法计算 BMI。如何从 main 调用方法。代码编译但没有给出所需的输出。我应该添加方法而不是类吗?输出只是要求我输入一些内容并终止。任何帮助都会有所帮助

class Program
{
    static void Main(string[] args)
    {       
        LbToKg Kgs = new LbToKg();
        InchesToMeters Mts = new InchesToMeters();
        BMIMetric BMiMet = new BMIMetric();
        BMICategory BMICat = new BMICategory();
        Console.ReadLine();
    }
    class LbToKg
    {
        public double LbsToKg(double lbs)
        {
            lbs = Convert.ToDouble(Console.ReadLine());
            double kilograms = lbs * 0.45359237;
            Console.WriteLine(lbs + " pounds is " + kilograms + " kilograms");
            Console.ReadLine();
            return kilograms;
        }
    }
    class InchesToMeters
    {
        public double InchToMeters(double inches)
        {
            inches = Convert.ToDouble(Console.ReadLine());
            double meters = inches / 39.37;
            Console.WriteLine(inches + " inches is " + meters + " meters");
            return meters;

        }
    }
    class BMIMetric
    {
        public double BMIMetrics(double kilograms, double meters)
        {
            double bmi = kilograms / (meters * meters);
            Console.WriteLine("Your BMI is:{0}", Math.Round(bmi, 4));
            return bmi;
        }
    }
    class BMICategory
    {
        public static void BMICategories(double bmi)
        {
            if (bmi < 18.5)
                Console.WriteLine("You are underweight.");
            else if (bmi > 18.5 && bmi < 24.9)
                Console.WriteLine("You're normal weight.");
            else if (bmi > 25.0 && bmi < 29.9)
                Console.WriteLine("You're Overweight.");
            else if (bmi > 30.0)
                Console.WriteLine("You are Obese"); 
        }
    }
}

标签: c#

解决方案


您有点偏离轨道:您使用方法定义类,并且仅使用这些类的构造函数,这没有做任何事情,而是您想调用您定义的方法。所以它们不应该被包装在任何类中,只是类上的方法Program。此外,它们应该是静态的,因为您想在static Main方法中调用它们。所以,你的代码应该是:

class Program
{
    static void Main(string[] args)
    {       
        double kgs = LbsToKg();
        double mts = InchToMeters();
        double bmiMet = BMIMetrics(kgs, mts);
        BMICategories(bmiMet);
        Console.ReadLine();
    }
    // you don't need paramteter - you collect it from the user
    public static  double LbsToKg()
    {
        double lbs = Convert.ToDouble(Console.ReadLine());
        double kilograms = lbs * 0.45359237;
        Console.WriteLine(lbs + " pounds is " + kilograms + " kilograms");
        Console.ReadLine();
        return kilograms;
    }
    // you don't need paramteter - you collect it from the user
    public static double InchToMeters()
    {
        double inches = Convert.ToDouble(Console.ReadLine());
        double meters = inches / 39.37;
        Console.WriteLine(inches + " inches is " + meters + " meters");
        return meters;

    }

    public static double BMIMetrics(double kilograms, double meters)
    {
        double bmi = kilograms / (meters * meters);
        Console.WriteLine("Your BMI is:{0}", Math.Round(bmi, 4));
        return bmi;
    }

    public static void BMICategories(double bmi)
    {
        if (bmi < 18.5)
            Console.WriteLine("You are underweight.");
        else if (bmi > 18.5 && bmi < 24.9)
            Console.WriteLine("You're normal weight.");
        else if (bmi > 25.0 && bmi < 29.9)
            Console.WriteLine("You're Overweight.");
        else if (bmi > 30.0)
            Console.WriteLine("You are Obese"); 
    }

}

推荐阅读