简体   繁体   English

如何在C#控制台中读取用户输入

[英]How to read user input in c# console

I guess this should be very simple to you guys, but very difficult to me because im new to c#. 我想这对你们来说应该很简单,但是对我来说却很难,因为我对C#还是陌生的。

I have a simple "pacient" class. 我有一个简单的“ pacient”课程。

public class Pacient {

public Pacient(string _name, string _lastName, DateTime _date, string _phone, string _email)
{
    name = _name;
    lastname = _lastName
    dateOfBirth = _date;
    phone_num = _phone;
    email = _email;
}


private string name;
public string Name {
    get {
        return name;
    }
    set {
        name = value;
    }
}

etc... 等等...

Now i want to read the input user types in console... 现在我想在控制台中读取输入的用户类型...

How do i do that? 我怎么做? It works with pre-typed names, like shown bellow.. 它可以使用预输入的名称,如下所示。

 Pacient John = new Pacient("John", " Doe ", new DateTime(1992,12,12) , " 045-999-333", "  example@example.com");
        John.Email = "example@example.com";
        John.Name ="JOHN ";
        John.LastName=" DOE ";*/

To sum up When console opens, it should ask for a name. 总结当控制台打开时,它应该要求一个名称。 And when user types in the name, console should store the name into "name" and later display it. 并且当用户键入名称时,控制台应将名称存储在“名称”中,然后再显示。

Thank you guys! 感谢大伙们!

One variable named name is not enough if you want to split it up into first and last name as provided in your example. 如果要将示例中提供的名字分为姓氏和name仅一个名为name变量是不够的。

Console.Write("First name:");
var firstName = Console.ReadLine();
Console.Write("Last name:");
var lastName = Console.ReadLine();

Pacient John = new Pacient(firstName, lastName, new DateTime(1992,12,12) , " 045-999-333", "  example@example.com");
John.Email = "example@example.com";

To print it: 要打印它:

Console.WriteLine("Name: {0} {1}",firstName,lastName);

PS Pa t ient is spelled with T in English. PS霸牛逼 ient是英文拼写有T。

Think you can find all the info you need right here. 认为您可以在这里找到所需的所有信息

string line = Console.ReadLine(); // Read string from console

Tip for the future: you already knew that this was called the console, because you used that word in the question. 面向未来的提示:您已经知道这称为控制台,因为您在问题中使用了该词。 So looking for 'C# console read text' on Google would be a good way to answer this question yourself. 因此,在Google上查找“ C#控制台读取文本”将是您自己回答此问题的好方法。 (Note: this is not flaming, just some feedback for the next question) (注意:这不是在燃烧,只是对下一个问题的一些反馈)

You can get user input via Console.Read(); 您可以通过Console.Read();获得用户输入Console.Read(); you need to get each user input 您需要获取每个用户的输入

Console.WriteLine("Enter First Name :");
string FirstName = Console.ReadLine();
 Console.WriteLine("What is your choice?:");
            string line = Console.ReadLine();

            switch (line)

{

case "1": // Do Something
break;

case "2": //Do that
}
 while (line != "9");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM