2017-12-03 61 views
0

我正在研究一個簡單的代碼,要求提供最多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) 
    { 

    } 
} 
+1

我投票關閉這一點,因爲你的國家,你不知道爲什麼它問你5次,但你調用方法'GetRecords(Record [] patient_rec'那麼你有一個for循環的長度記錄[]你期望什麼..?我強烈建議你學習如何使用調試器,設置斷點和步驟通過代碼.. – MethodMan

+0

你也在調用'GetRecords()再次在開關ch聲明..這需要一些認真的重新思考/調試.. – MethodMan

+0

@MethodMan我們需要創建一個包含5個位置的數組,所以部分不能改變。是否有可能將for循環移動到另一個位置,以便每次更新? –

回答

0

你的循環設置爲僅轉到數組的大小,因此在邏輯上你可以展示在循環後的消息(這將讓循環完成時命中)。

如果你控制一個while循環的數組訪問那麼你的索引i只是比較的陣列(patient_rec.Length)的長度,如果等於或超過,則長度顯示信息。

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(); 
    } 
} 
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."); 
} 
+0

沒有看到問題。什麼是問題,downvoter? –

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; 
}