如何在C#控制台中读取用户输入 [英] How to read user input in c# console

查看:172
本文介绍了如何在C#控制台中读取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这对你们来说应该很简单,但是对我来说却很难,因为我是C#的新手。

I guess this should be very simple to you guys, but very difficult to me because im new to c#.

我有一个简单的 pacient类。 / p>

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;
    }
}

等...

现在我想在控制台中读取输入的用户类型...

Now i want to read the input user types in console...

我该怎么做?

 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.

谢谢大家!

推荐答案

仅提供一个名为 name 的变量,如果您想将其拆分为名字和姓氏

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";

要打印:

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

P.S。 Pa t ient用英语拼写为T。

P.S. Patient is spelled with T in English.

这篇关于如何在C#控制台中读取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆