简体   繁体   中英

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#.

I have a simple "pacient" class.

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.

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.

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. (Note: this is not flaming, just some feedback for the next question)

You can get user input via 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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