2017-12-03 6 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ループをRecord []の長さにします。何を期待していますか?デバッガの使い方、ブレークポイントの設定、コードを介してステップ.. – MethodMan

+0

また、 'GetRecords()を再度呼び出すchステートメント..これは深刻な再考/デバッグが必要です。 – MethodMan

+0

@MethodMan 5つの場所を持つ配列を作成する必要があります。そのため、一部を変更することはできません。 forループを別の場所に移動して、毎回更新できるようにすることは可能ですか? –

答えて

0

あなたのループのみを配列のサイズに行くために設定されているので、論理的に、あなたは(ループが終了すると、これが打撃を受けるだろう)ループの後にメッセージを表示することができます。

あなたはwhileループでの配列アクセスを制御した場合は、ちょうどそれが長さに等しいか、超えた場合に、メッセージを表示し、配列(patient_rec.Length)の長さにあなたのインデクサiを比較します。

私は簡単な方法でそれを行うだろう
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

問題は表示されません。問題は何ですか、下降者ですか? –

1

あなたのコードを少し読みやすくするために、より堅牢にすることをお勧めします。あなたはこれらの3つの入力機能を必要とし、この作業を行うには

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; 
} 
関連する問題