如何接受用户输入并打印详细信息

本文关键字:打印 详细信息 输入 何接受 用户 | 更新日期: 2023-09-27 18:36:12

我正在开发考试应用程序(c# 控制台应用程序)

该应用程序要求用户输入其名称,我希望该应用程序读取该用户的名称,并且根据我存储在对象中的详细信息打印用户的详细信息

例如:如果用户的名称与此对象中的名称匹配:

students s3 = new students("Dee", "Scott", "Computing", 100m, 66.6m);

它怎么能打印这个用户的详细信息。

我有一个单独的方法来打印用户的详细信息

public string gradeDetails {
        get { return FirstName + LastName + Course + FinalGrade; }
    }

我不知道如何将用户输入与相应的对象相匹配。

如何接受用户输入并打印详细信息

您可以使用 Console.Readline() 方法

   public static void Main()
   {
      string line;
      Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");
      Console.WriteLine();
      do { 
         Console.Write("   ");
         line = Console.ReadLine();
         if (line != null) 
            Console.WriteLine("      " + line);
      } while (line != null);  
Console.ReadLine();
Console.WriteLine(); or Console.Write(); 

我不确定这是否是你要问的,但这些是阅读和写作的呼吁。

不过,这些信息很容易被谷歌搜索。

下面的示例

将执行您的要求。如果您对我使用的代码有任何疑问,请随时询问。

class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Course { get; set; }
    public decimal FinalGrade { get; set; }
    public Student(string firstName,
                   string lastName,
                   string course)
    {
        FirstName = firstName;
        LastName = lastName;
        Course = course;
        FinalGrade = 0;
    }
    // This will first call the constructor above and then continue.
    public Student(string firstName,
                   string lastName,
                   string course,
                   decimal finalGrade) : this(firstName, lastName, course)
    {
        FinalGrade = finalGrade;
    }
    // By overriding ToString we can use Console.WriteLine(student) directly.
    public override string ToString()
    {
        return string.Format(@"FirstName: {0}, LastName: {1}, Course: {2}, FinalGrade: {3}",
                             FirstName,
                             LastName,
                             Course,
                             FinalGrade);
    }
}
class Program
{
    static void Main(string[] args)
    {
        // Create our students.
        List<Student> students = new List<Student>
                                 {
                                     new Student("John", "Test", "Computing"),
                                     new Student("Tim", "Test", "Computing", 8.25m)
                                 };
        string user = "";
        do
        {
            Console.Clear();
            Console.WriteLine("Please enter the name of the student:");
            user = Console.ReadLine();
            if (user.Equals("exit", StringComparison.OrdinalIgnoreCase))
                break;
            // Find the student or return null.
            Student student = students.FirstOrDefault(s => s.FirstName.Equals(user, StringComparison.OrdinalIgnoreCase));
            if (student != null)
            {
                Console.WriteLine("Student info:");
                Console.WriteLine(student);
            }
            else
            {
                Console.WriteLine("Student '" + user + "' not found.");
            }
            Console.WriteLine();
            // Wait until a key is pressed.
            Console.WriteLine("Press a key to continue..");
            Console.ReadKey(true);
        } while (true);
    }
}

做这样的事情:创建一个名为"学生"的课程:

 public class Student
    {
        public string Fname { get; set; }
        public string LName { get; set; }
        public string Course { get; set; }
        public string FinalGrade { get; set; }
    }

然后这样做

class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter you name:");
            string name = Console.ReadLine();
            Console.WriteLine(gradeDetails(name));
            Console.ReadLine();
        }
        public static string gradeDetails(string name)
        {
             List<Student> students = new List<Student>()
                {
                    new Student{ Fname = "Scott",LName ="Dee",Course = "Computing", FinalGrade = "66.66m"},
                    new Student{Fname = "Joe",LName = "Don",Course = "Chemestry", FinalGrade = "80.77m"}
                };
            var student = students.SingleOrDefault(s => s.LName.ToLower() == name.ToLower());
            if (student!=null)
            {
                return student.Fname + "" + student.LName + "" + student.Course + "" + student.FinalGrade;
            }
            return string.Empty;
        }
    }

要求用户输入姓名和打印姓名

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace stackoverflow1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string name;
                Console.WriteLine("Enter your name : ");
                name = Console.ReadLine();
                Console.WriteLine("Hello " + name + " , Welcome to OOP!");
                Console.ReadKey();
            }
        }
    }