我在我的介绍课程中有一个作业,我需要提示用户输入名字,姓氏,出生年份和当前年份。该程序需要将信息与计算的年龄,最大心率(220-年龄)和目标心率范围(50%至85%)一起输出回用户。我的类应该有一个构造函数,它接收输入的数据作为参数。每个属性都应该有get / sets,并且应该有属性来计算年龄,最大心率和目标范围。这就是我到目前为止所拥有的。我想我已经完成了它所要求的但是我不确定在调用构造函数时要使用什么args。或许我在这里误解了一个概念。
提前感谢任何建议。
using System;
namespace Assignment2
{
public class HeartRates
{
private string firstname;
private string lastname;
private int birthyear;
private int currentyear;
private int age;
private double maxrate;
private double mintarget;
private double maxtarget;
public string Firstname
{
get { return firstname; }
set { firstname = value; }
}
public string Lastname
{
get { return lastname; }
set { lastname = value; }
}
public int Birthyear
{
get { return birthyear; }
set { birthyear = value; }
}
public int Currentyear
{
get { return currentyear; }
set { currentyear = value; }
}
public int Age
{
get { return currentyear - birthyear; }
set { age = currentyear - birthyear; }
}
public double MaxRate
{
get { return 220 - Age; }
set { maxrate = value; }
}
public double MinTarget
{
get { return MaxRate * .5; }
set { mintarget = value; }
}
public double MaxTarget
{
get { return MaxRate * .85; }
set { maxtarget = maxrate * .85; }
}
public HeartRates(string firstname, string lastname, int birthyear, int currentyear)
{
Console.WriteLine("Please enter your first name:");
this.firstname = Console.ReadLine();
Console.WriteLine("Please enter your last name:");
this.lastname = Console.ReadLine();
Console.WriteLine("Please enter your birth year:");
this.birthyear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the current year:");
this.currentyear = Convert.ToInt32(Console.ReadLine());
}
}
public class Program
{
public static void Main()
{
HeartRates newheartrates = new HeartRates();
Console.WriteLine("Your name is {0} {1}, you were born in {2}, the current year is {3}", newheartrates.Firstname, newheartrates.Lastname, newheartrates.Birthyear, newheartrates.Currentyear);
Console.WriteLine("Your age is {0}", newheartrates.Age);
Console.WriteLine("Your maximum heart rate is {0}", newheartrates.MaxRate);
Console.WriteLine("Your target heart rate range is from {0} to {1}.", newheartrates.MinTarget, newheartrates.MaxTarget);
}
}
}
答案 0 :(得分:0)
你走在正确的轨道上,但我觉得你有点困惑。
你应该移动这部分
Console.WriteLine("Please enter your first name:");
this.firstname = Console.ReadLine();
Console.WriteLine("Please enter your last name:");
this.lastname = Console.ReadLine();
Console.WriteLine("Please enter your birth year:");
this.birthyear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the current year:");
this.currentyear = Convert.ToInt32(Console.ReadLine());
到你的主要功能。
然后使用您创建的构造函数。
HeartRates newheartrates = new HeartRates(firstname, lastname, birthyear, currentyear);
你有什么可以工作,但1)你没有调用你创建的构造函数,你正在调用没有参数的构造函数。通常不希望在对象构造函数中输入用户。在main函数中执行它并将输入/输出保持在一起更有意义。
希望这会有所帮助,欢迎来到Stackoverflow。
答案 1 :(得分:0)
首先获取用户输入(在main方法内部,而不是在构造函数中):
Console.WriteLine("Please enter your first name:");
string firstname = Console.ReadLine();
Console.WriteLine("Please enter your last name:");
string lastname = Console.ReadLine();
Console.WriteLine("Please enter your birth year:");
int birthyear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the current year:");
int = currentyear = Convert.ToInt32(Console.ReadLine());
然后调用你的构造函数:
HeartRates newheartrates = new HeartRates(firstname,lastname,birthyear,currentyear);
然后改变你的构造函数:
public HeartRates(string firstname, string lastname, int birthyear, int currentyear)
{
this.firstname = firstname;
this.lastname = lastname;
this.birthyear = birthyear;
this.currentyear = currentyear;
}