【问题标题】:Can I record 2 things in an Array List in C#?我可以在 C# 的数组列表中记录 2 件事吗?
【发布时间】:2016-01-14 10:53:25
【问题描述】:

为了我的家庭作业,我必须用 C# 创建一个程序,它有一个名为“耐心”的类,如下所示:

class patient
{
    private string name;
    private int age;
    private double weight;
    private double height;

    public patient()
    {
        name = "";
        age = 0;
        weight = 0;
        height = 0;
    }

    public patient(string newName, int newAge, double newWeight, double newHeight)
    {
        name = newName;
        age = newAge;
        weight = newWeight;
        height = newHeight;
    }

    public double bmi()
    {
        return weight / Math.Pow(height, 2);
    }

    public bool obese()
    {
        if (bmi() > 27 && age < 40)
            return true;
        else if (bmi() > 30 && age >= 40)
            return true;
        else
            return false;
    }

    public void printDetails()
    {
        Console.WriteLine("Name: " + name);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Weight: " + weight + "kg");
        Console.WriteLine("Height: " + height + "m");
        Console.WriteLine("BMI: " + bmi());
        if (obese())
            Console.WriteLine("Patient is obese");
        else
            Console.WriteLine("Patient is not obese.");
    }

问题的最后一部分说: 编写一个方法,将最近输入的患者连同他们的肥胖诊断记录到一个 ArrayList 中。它应该记录五个最近的条目及其诊断。

数组列表不能是多维的,但问题是要我记录肥胖诊断和实际患者。

我曾考虑将对象存储在数组列表中,但我不确定这是否是问题想要的。有什么想法吗?

【问题讨论】:

  • 你能问问你的老师/助教吗?这很不清楚。
  • 有源列表吗?是否有某个来源包含所有患者,您必须从中选择符合标准的患者?
  • 您可以将他们的肥胖诊断结果存储在患者对象本身中

标签: c# arrays class arraylist


【解决方案1】:

我会做以下事情:

// you will have a history of Records
// a Record contains the Patient + obesity result
public class Record 
{
   public Patient Patient {get; private set;} 
   public bool ObesityResult {get; private set; }
   public Record(Patient patient, bool obesityResult) 
   {
       this.Patient = patient; 
       this.ObesityResult = obesityResult; // save the obese result
   }
}


// now this class will handle the history.
public class RecordHistory 
{
    private ArrayList history; 

    public void Add(Patient patient) 
    { 
        var record = new Record(patient, patient.obese());  // pass the obesity result
        history.Add(patient);  // DO some magic here to keep only 5
    }

   public ArrayList GetHistory() 
   {
      return history;
   }
}

不要把它当作你在现实生活中应该怎么做的例子。这只是一个家庭作业。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 2010-12-09
    • 1970-01-01
    相关资源
    最近更新 更多