我正在使用4种方法来计算BMI。我如何从主体中调用方法。该代码可以编译,但不能提供所需的输出。我应该添加方法而不是类吗?输出仅要求我输入内容并终止。任何帮助都会帮助
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");
}
}
}
答案 0 :(得分:3)
您有点偏离轨道:您使用方法定义类,并且仅使用这些类的构造函数,该类不执行任何操作,而是要调用您定义的方法。因此,它们不应包装在任何类中,而只能包装在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");
}
}
答案 1 :(得分:1)
我建议在单个类中实现业务逻辑:
public class Bmi {
public Bmi(double inches, double lbs) {
if (inches <= 0.0)
throw new ArgumentOutOfRangeException(nameof(inches));
else if (lbs <= 0.0)
throw new ArgumentOutOfRangeException(nameof(lbs));
Inches = inches;
Lbs = lbs;
}
public double Lbs {get;}
public double Kg => Lbs * 0.45359237;
public double Inches {get;}
public double Meters => Inches / 39.37;
public double Index => Kg / (Meters * Meters);
public string Description {
get {
double bmi = Index;
if (bmi < 18.5)
return "underweight";
else if (bmi < 24.9)
return "normal weight";
else if (bmi < 29.9)
return "overweight";
else
return "obese";
}
}
public override string ToString() => Description;
}
我们也提取ReadDouble
:
public static double ReadDouble(string title) {
Console.WriteLine(title);
while (true) {
if (double.TryParse(Console.ReadLine(), out double result))
return result;
Console.WriteLine("Sorry, invalid syntax. Please, provide floating point value.");
}
}
然后您可以使用它:
static void Main(string[] args) {
Bmi bmi = new Bmi(
ReadDouble("Please, enter your height in inches:"),
ReadDouble("Please, enter your weight in lbs:")
);
Console.WriteLine($"You are {bmi.Description}");
Console.ReadKey();
}
请注意,您可以轻松提供更多信息,例如
Console.WriteLine($"You are {bmi.Description} with BMI index {bmi.Index:f2}");
答案 2 :(得分:0)
您没有调用任何方法,请尝试如下操作:
public static void Main(string[] args) {
var bmi = BMIMetric.BMIMetrics(70, 170);
BMICategory.BMICategories(bmi);
}
答案 3 :(得分:0)
这应该对您有用。
static void Main(string[] args)
{
double kilograms = ConsoleConvertors.LbsToKg();
double Mts = ConsoleConvertors.InchToMeters();
double BMiMet = ConsoleConvertors.BMIMetrics();
double BMICat = ConsoleConvertors.BMICategories();
}
public static class ConsoleConvertors
{
public static double LbsToKg()
{
Console.WriteLine("LbsToKg - Enter lsb:");
var lbs = Convert.ToDouble(Console.ReadLine());
double kilograms = lbs * 0.45359237;
Console.WriteLine(lbs + " pounds is " + kilograms + " kilograms");
return kilograms;
}
public static double InchToMeters()
{
Console.WriteLine("InchToMeters - Enter inch:");
var inches = Convert.ToDouble(Console.ReadLine());
double meters = inches / 39.37;
Console.WriteLine(inches + " inches is " + meters + " meters");
return meters;
}
public static double BMIMetrics( )
{
Console.WriteLine("BMIMetrics - Enter kilograms :");
var kilograms = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("BMIMetrics - Enter meters :");
var metter = Convert.ToDouble(Console.ReadLine());
double bmi = kilograms / (metter * metter);
Console.WriteLine("Your BMI is:{0}", Math.Round(bmi, 4));
return bmi;
}
public static double BMICategories()
{
Console.WriteLine("BMICategories - Enter bmi :");
var bmi = Convert.ToDouble(Console.ReadLine());
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");
return bmi;
}
}
答案 4 :(得分:0)
这里发生的事情是您正在创建对象Kgs
,Mts
等,但从未调用方法LbsToKg
,InchToMeters
等。要调用它们,您将使用Kgs.LbsToKgs();
和Mts.InchToMeters();
。
通常,对于此类程序,您将使用不带单独类的方法。这是编写程序时更改最少的一种方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bmiCalculator
{
class Program
{
static void Main(string[] args)
{
double kg = LbsToKg();
double m = InchToMeters();
double bmi = BMIMetrics(kg, m);
BMICategories(bmi);
Console.ReadLine();
}
public static double LbsToKg()
{
Console.Write("Weight in lbs: ");
double lbs = Convert.ToDouble(Console.ReadLine());
double kilograms = lbs * 0.45359237;
Console.WriteLine(lbs + " pounds is " + kilograms + " kilograms");
return kilograms;
}
public static double InchToMeters()
{
Console.Write("Height in inches: ");
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");
}
}
}