如何显示阵列已满

时间:2017-12-03 03:03:02

标签: c#

我正在编写一个简单的代码,询问最多5名患者的姓名,年龄和性别。在每位患者之后,应该要求输入另一位患者或返回主菜单。一旦将5输入到数组中,就应该提示用户该数组已满。

我的问题是代码提前5次询问姓名,年龄和性别,并且没有任何迹象表明数组已满。我如何更改代码以反映并仍然保存输入? (以下代码)。

class MainClass
{
    enum Gender { female, male }
    struct Record
    {
        public string _Name;
        public int _Age;
        public Gender _Gender;
    }

    public static void Main(string[] args)
    {
        //title 
        Console.Write("\t\t\t\t\tPatient Records\n");
        string selection = "";
        Record[] patients = new Record[5];
        GetRecords(patients);
        Console.Write("a. Add\n d.Display\ns. Stats\nq. Quit");
        Console.Write("Your selection: ");
        selection = Console.ReadLine();
        switch (selection)
        {
            case "a":
                GetRecords(patients);
                break;
            case "d":
                break;
            case "s":
                Stats(patients);
                break;
            case "q":
                //CUtility.Pause();
                break;
        }
    }

    static void GetRecords(Record[] patient_rec)
    {
        for (int i = 0; i < patient_rec.Length; i++)
        {
            Console.Write("Enter your age: ");
            int.TryParse(Console.ReadLine(), out patient_rec[i]._Age);
            Console.Write("Enter your name: ");
            patient_rec[i]._Name = Console.ReadLine();
            Console.Write("Enter your gender (female or male): ");
            Gender.TryParse(Console.ReadLine(), out patient_rec[i]._Gender);
        }
    }

    static void Stats(Record[]patient_rec)
    {

    }
}

4 个答案:

答案 0 :(得分:1)

我建议您尝试使代码更容易阅读 - 并且更加强大。

试试这个:

static void GetRecords(Record[] patient_rec)
{
    for (int i = 0; i < patient_rec.Length; i++)
    {
        Console.WriteLine("Record {0} of {1} entry", i + 1, patient_rec.Length);
        patient_rec[i] = new Record()
        {
            _Age = AskInteger("Enter your age: "),
            _Name = AskString("Enter your name: "),
            _Gender = AskGender("Enter your gender (female or male): "),
        };
        string ask = "";
        while (string.IsNullOrEmpty(ask) || (ask.ToLower()[0] != 'y' && ask.ToLower()[0] != 'n'))
        {
            Console.WriteLine("Continue? yes or no (then hit enter)");
            ask = Console.ReadLine();
        }
        if (ask.ToLower()[0] == 'y')
        {
            continue;
        }
        break;
    }
    Console.WriteLine("Thank you. Input completed.");
}

要完成这项工作,您需要以下三个输入功能:

private static int AskInteger(string message)
{
    int result;
    Console.WriteLine(message);
    string input = Console.ReadLine();
    while (!int.TryParse(input, out result))
    {
        Console.WriteLine("Invalid input.");
        Console.WriteLine(message);
        input = Console.ReadLine();
    }
    return result;
}

private static string AskString(string message)
{
    Console.WriteLine(message);
    string input = Console.ReadLine();
    while (string.IsNullOrWhiteSpace(input))
    {
        Console.WriteLine("Invalid input.");
        Console.WriteLine(message);
        input = Console.ReadLine();
    }
    return input;
}   

private static Gender AskGender(string message)
{
    Gender result;
    Console.WriteLine(message);
    string input = Console.ReadLine();
    while (!Gender.TryParse(input, out result))
    {
        Console.WriteLine("Invalid input.");
        Console.WriteLine(message);
        input = Console.ReadLine();
    }
    return result;
}

答案 1 :(得分:0)

你的循环只设置为数组的大小,所以逻辑上你可以在循环后显示一条消息(这将在循环结束时被点击)。

如果您在while循环中控制数组访问权限,那么只需将索引器i与数组的长度(patient_rec.Length)进行比较,如果它等于或超过长度,则显示信息。

答案 2 :(得分:0)

我会以更简单的方式做到这一点:

  enum Gender { female, male }
    struct Record
    {
        public string _Name;
        public int _Age;
        public Gender _Gender;
    }

    static void Main(string[] args)
    {
        //title 
        Console.Write("\t\t\t\t\tPatient Records\n");
        IList<Record> patients = GetRecords(5);
        SchedulePatients(patients);
    }

    static void SchedulePatients(IList<Record> patients)
    {
        Console.Write("a. Add\n d.Display\ns. Stats\nq. Quit");
        Console.Write("Your selection: ");
        string selection = Console.ReadLine();
        switch (selection)
        {
            case "a":
                patients.Add(GetRecord());
                SchedulePatients(patients);
                break;
            case "d":
                break;
            case "s":
                Stats(patients);
                break;
            case "q":
                //CUtility.Pause();
                break;
        }
    }

    static IList<Record> GetRecords(int amount)
    {
        IList<Record> patients = new List<Record>();

        for (int i = 0; i < amount; i++)
        {
            patients.Add(GetRecord());
        }

        return patients;
    }

    static Record GetRecord()
    {
        Record patient = new Record();
        Console.Write("Enter your age: ");
        int.TryParse(Console.ReadLine(), out patient._Age);
        Console.Write("Enter your name: ");
        patient._Name = Console.ReadLine();
        Console.Write("Enter your gender (female or male): ");
        Enum.TryParse(Console.ReadLine(), out patient._Gender);

        return patient;
    }

    static void Stats(IList<Record> patients)
    {
        foreach (var patient in patients)
        {
            Console.WriteLine(string.Concat("Name: ", patient._Name, " Age: ", patient._Age, " Gender: ", patient._Gender));
        }
        Console.ReadLine();
    }
}

答案 3 :(得分:0)

如果您希望以尽可能小的变化满足要求,您只需要添加有关提示用户的信息。

static void GetRecords(Record[] patient_rec)
{
    for (int i = 0; i < patient_rec.Length; i++)
    {
        Console.Write("Enter your age: ");
        int.TryParse(Console.ReadLine(), out patient_rec[i]._Age);
        Console.Write("Enter your name: ");
        patient_rec[i]._Name = Console.ReadLine();
        Console.Write("Enter your gender (female or male): ");
        Gender.TryParse(Console.ReadLine(), out patient_rec[i]._Gender);
        Console.Write("Enter another (Y/N)? ");
        var s = Console.ReadLine();
        if (s.ToUpper() != "Y") return;
   }
   Console.WriteLine("You've entered the maximum number of records.");
}