c# - 如何从另一个类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");
}
}
}
解决方案
您有点偏离轨道:您使用方法定义类,并且仅使用这些类的构造函数,这没有做任何事情,而是您想调用您定义的方法。所以它们不应该被包装在任何类中,只是类上的方法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");
}
}
推荐阅读
- r - 使用按类别的增长率填充 data.table 中的缺失值
- c++ - 继承和覆盖虚拟赋值 == 运算符
- java - 范围报告框架-如何获取存储在范围报告日志功能中的值
- java - 从容器内部连接到 PostgreSQL 容器的问题
- json - URLSession.shared.dataTask 返回零字节
- oracle - 我正在尝试使用一个程序每分钟自动更新一次顶点日历
- python - 追加列表的累积列表python
- php - 如何在 Laravel 5.7 中将数据插入到多对多表中
- azure-active-directory - 端点 /users/{userId}/drive/recent 返回空数组
- powershell - 从命令行执行 powershell 脚本时出现空格问题