【问题标题】:Write Line error in C#在 C# 中写入行错误
【发布时间】:2018-02-26 20:59:51
【问题描述】:

我的任务是在 C# 中创建一个记录保存控制台应用程序,用于添加、查看和删除记录。我需要使用数组、列表或字典(或某种组合)。

我尝试过使用数组和列表,但到目前为止,字典被证明是最成功的(我知道如何在这个程序中最好地使用它们)。照片中是我到目前为止的代码,但是当我提示用户输入信息时出现错误。我尝试了一些不同的修复方法,但都没有奏效。

My Code

    class Patient
    {
        public static string PatientDict;
            public Console.WriteLine("Enter the patient's name: "); 
        string PatientName = Console.ReadLine();
            public Console.WriteLine("Enter the patient's date of birth: "); 
        string PatientDOB = Console.ReadLine();
            public Console.WriteLine("Enter the patient's age: "); 
        string PatientAge = Console.ReadLine();
            public Console.WriteLine("Enter the patient's ailment(s): "); 
        string PatientAilments = Console.ReadLine();
            public Console.WriteLine("Enter the patient's medication(s): "); 
        string PatientMeds = Console.ReadLine();

        string DOB;
        string Age;
        string Ailments;
        string Meds;
    }

【问题讨论】:

  • public Console.WriteLine("Enter the patient's name: ");?我认为您需要继续阅读 c# 教程,这根本无效
  • 创建一个方法来放置代码,你把字段和语句混淆了。
  • 根据我在课堂上的记忆,Console.WriteLine();我的教授用的是什么,我在网上找到了什么? (我添加了 public 作为随机修复尝试)
  • 您的教授可能确实使用了Console.WriteLine();只有正确。不要试图通过随机更改来修复代码;编程太复杂了,无法通过这种方式学习。反而; read tutorials 和[文档(msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx)。

标签: c# error-handling console syntax-error console-application


【解决方案1】:

您可以像这样使用对象(患者)的列表:

class Program
{
    static void Main(string[] args)
    {
        // Create a new list of patients to store your values
        List<Patient> patients = new List<Patient>();

        // Get user input and store them as variables
        Console.WriteLine("Enter the patients name: ");
        string name = Console.ReadLine();

        Console.WriteLine("Enter the patients date of birth: ");
        string dob = Console.ReadLine();

        Console.WriteLine("Enter the patients age: ");
        int age = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the patients ailment(s): ");
        string ailments = Console.ReadLine();

        Console.WriteLine("Enter the patients medication(s): ");
        string meds = Console.ReadLine();

        Console.WriteLine(); // Empty line


        // Add the given input to the list of patients
        patients.Add(new Patient { patientName = name, patientDOB = dob, patientAilments = ailments, patientMeds = meds, patientAGE = age });


        // Print patients to console output
        foreach (var patient in patients)
        {
            Console.WriteLine("Patients name = {0}", patient.patientName);
            Console.WriteLine("Patients date of birth = {0}", patient.patientDOB);
            Console.WriteLine("Patients age = {0}", patient.patientAGE);
            Console.WriteLine("Patients ailment(s) = {0}", patient.patientAilments);
            Console.WriteLine("Patients medication(s) = {0}", patient.patientMeds);
        }
    }
}

class Patient
{
    public string patientName { get; set; }
    public string patientDOB { get; set; }
    public string patientAilments { get; set; }
    public string patientMeds { get; set; }
    public int patientAGE { get; set; }
}

这只是做你想做的事的众多方法之一。如果您想输入更多患者,只需提示一组新问题并将其添加到列表中即可。

【讨论】:

    【解决方案2】:

    您可能想要重写您的程序。您有多个Main 方法的案例,而Console.WriteLine(...) 不是方法级代码。

    看起来您想在 main 方法中执行所有这些操作,因此请考虑:

    class Patient 
    {
        static void main(string[] args) 
        {
            Console.WriteLine("Enter the patient's name: "); 
            string PatientName = Console.ReadLine();
    
            Console.WriteLine("Enter the patient's date of birth: "); 
            string PatientDOB = Console.ReadLine();
    
            Console.WriteLine("Enter the patient's age: "); 
            string PatientAge = Console.ReadLine();
    
            Console.WriteLine("Enter the patient's ailment(s): "); 
            string PatientAilments = Console.ReadLine();
    
            Console.WriteLine("Enter the patient's medication(s): "); 
            string PatientMeds = Console.ReadLine();
        }
    }
    

    作为起点,阅读更多关于C# & tutorials的信息

    【讨论】:

    • 应该有 2 个类:包含执行用户交互的 Main 方法的 Program 类和仅具有属性的 Patient 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2014-12-28
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多