我正在研究一個簡單的代碼,要求提供最多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)
{
}
}
我投票關閉這一點,因爲你的國家,你不知道爲什麼它問你5次,但你調用方法'GetRecords(Record [] patient_rec'那麼你有一個for循環的長度記錄[]你期望什麼..?我強烈建議你學習如何使用調試器,設置斷點和步驟通過代碼.. – MethodMan
你也在調用'GetRecords()再次在開關ch聲明..這需要一些認真的重新思考/調試.. – MethodMan
@MethodMan我們需要創建一個包含5個位置的數組,所以部分不能改變。是否有可能將for循環移動到另一個位置,以便每次更新? –