您好,感谢您查看我的帖子。一般是 C# 和 Oop 的新手。到目前为止学习一直很顺利,但是当我尝试创建(和调用)一个计算用户输入的数据的构造函数时遇到了问题。
如果可能:我想创建 3 个类。
显示信息并获取用户输入的 MainDisplay 类。
“仅”计算 bmi 的 BMIBlueprint 类。
计算 bfp 的 BFPBlueprint 类(需要使用 bmi total)。
我已经使用一个类完成了这项任务,但我真的很想知道/理解如何使用多个类来做到这一点。在提出这个问题之前,我已经看过/阅读/研究了过去两周我能找到的所有资源,但我无法理解创建一个进行计算的构造函数,然后在 main 中调用它以使用用户输入数据对于变量..
最底层的代码是我如何在一节课中完成的(但还没有完成)。不过,您可以无视所有这些,我只是不想表现得像我完全绝望或试图作弊一样。
这就是我到目前为止所拥有的。第一个是我创建的 BMIBlueprint 类,它只是用作基于用户输入的 bmi 计算器,我计划在我的 MainClass 中调用它。此外,这大约是我尝试使其工作的第 200 个不同版本,它可能与它可能/应该看起来的样子相去甚远......
class BMIBlueprint {
public double bmiBefore;
public double bmiAFter;
public BMIBlueprint() { }
public BMIBlueprint(double height, double weight) {
bmiBefore = (height * height) * weight;
}
public BMIBlueprint(double bmiConst) {
bmiAFter = bmiBefore * 703;
}
}
如何“使用”上述类构造函数来计算用户在我的主类中输入的内容?还是我偏离了轨道?我的主类在我要求用户输入的位置下方,然后将这些值放在我的构造函数中。
static void Main() {
Write("Enter weight: ");
double weight = double.Parse(ReadLine());
Write("Enter height: ");
double height = double.Parse(ReadLine());
BMIBlueprint calcBmi = new BMIBlueprint(newBmiOne, newBmiTwo);
//then display after calculated
WriteLine("The finished cal should be shown here: {0:N2}", calcBmi);
我已经尝试了很多不同的方法,但这就是我现在所处的位置;如果有人有时间或善意地用一些笔记发布代码应该是什么,以便我可以研究它,那绝对很棒,但也可能要求太多。如果没有任何帮助/建议/隐喻/类比将不胜感激。
下面的代码是我在一节课中完成的所有工作 - 几乎完成,但不是问题的一部分,应该被忽略。
static void Main() {
EntranceBlueprint entrance = new EntranceBlueprint();
WriteLine(entrance);
BMIBlueprint bmiCalculator = new BMIBlueprint();
Write("First, enter your weight in lbs: ");
double newWeight = double.Parse(bmiCalculator.weight = ReadLine());
Write("\nGreat, now enter your height in inches: ");
double newHeight = double.Parse(bmiCalculator.height = ReadLine());
bmiCalculator.bmiVar = 703;
double totalBmi = ((newWeight * bmiCalculator.bmiVar) /
(newHeight * newHeight));
WriteLine("\nPerfect! Your bmi is: {0:N2}. With just a few more
inputs we'll have your bfp. ", +totalBmi);
Write("\npress any key to continue");
ReadKey();
BFPBlueprint bfpCalculator = new BFPBlueprint();
Write("According to _ your bfp is more accurate because it takes
into account your age and gender. So enter your age first: ");
double newAge = double.Parse(bfpCalculator.age = ReadLine());
Write("\nGreat, now enter your sex, like this (male or female): ");
string newSex = ReadLine();
if (newAge >= 13 && newSex.ToLower() == "male") {
double totalAdultMale =
(1.20 * totalBmi) - (0.23 * newAge) - (10.8 * 1) - 5.4;
Write("\nYour bfp is: {0:N2} ", totalAdultMale);
if (totalAdultMale > 1 && totalAdultMale < 13)
Catagories(); }
else if (newAge <= 12 && newSex.ToLower() == "male") {
double totalYouthMaleBfp =
(1.51 * totalBmi) - (0.70 *newAge) - (3.6 * 1) + 1.4;
Write("Perfect! Your bfp is: {0:N2}",
else if (newAge >= 13 && newSex.ToLower() == "female") {
double totalAdultFemaleBfp =
(1.20 * totalBmi) - (0.23 * newAge) - (10.8 * 0) - 5.4;
Write("Perfect! Your bfp is: {0:N2} ",totalAdultFemaleBfp); }
else if (newAge <= 12 && newSex.ToLower() == "female") {
double totalYouthFemaleBfp =
(1.51 * totalBmi) - (0.70 * newAge) - (3.6 * 0) + 1.4;
Write("Perfect! Your bfp is {0:N2} ", totalYouthFemaleBfp); }
else { Write("\nYou must have typed something wrong, Try
again"); }
ReadKey();