在这种情况下是否有另一种添加 Console.WriteLine() 的替代方法
Is there another alternate way to add Console.WriteLine() in this scenario
在这段代码中,我希望在每个 Console.ReadLine() 之后添加一个 Console.Writeline() 但是我如何实现代码却给我一个错误。还有另一种方法可以在实例中添加 Console.WriteLine() 吗?
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create")
{
NameOfUser = Console.ReadLine(),
SurnameOfUser = Console.ReadLine(),
UserID = Console.ReadLine(),
UserEmail = Console.ReadLine(),
UserHomeAdd = Console.ReadLine(),
UserMobileNumber = int.Parse(Console.ReadLine()),
UsernameField = Console.ReadLine(),
PasswordField = Console.ReadLine(),
CoffePoints = int.Parse(Console.ReadLine())
};
List<Client> accountData = new List<Client>()
{
createAccount
};
您可以创建一个方法来打印某些内容和 returns Console.ReadLine(),例如:
private static string ReadLine(string writeMessage, bool parseAsInt = false)
{
Console.WriteLine(writeMessage);
var line = Console.ReadLine();
if (parseAsInt)
{
int parseInt = 0;
int.TryParse(line, out parseInt);
line = parseInt.ToString();
}
return line;
}
那么在创建对象的时候调用即可:
Client createAccount = new Client("Create")
{
NameOfUser = ReadLine("What's your name?"),
SurnameOfUser = ReadLine("Input your surname"),
[...]
CoffePoints = ReadLine("Coffe points?", true)
};
您不能将 WriteLine() 放在 ReadLine() 之间,因为您正在初始化新客户端的属性。但是,您可以这样做:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
Console.WriteLine("Enter NameOfUser ");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Enter SurnameOfUser ");
createAccount.SurnameOfUser = Console.ReadLine();
Console.WriteLine("Enter UserID ");
createAccount.UserID = Console.ReadLine();
Console.WriteLine("Enter UserEmail ");
createAccount.UserEmail = Console.ReadLine();
Console.WriteLine("Enter UserHomeAdd ");
createAccount.UserHomeAdd = Console.ReadLine();
Console.WriteLine("Enter UserMobileNumber ");
createAccount.UserMobileNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Enter UsernameField ");
createAccount.UsernameField = Console.ReadLine();
Console.WriteLine("Enter PasswordField ");
createAccount.PasswordField = Console.ReadLine();
Console.WriteLine("Enter CoffePoints ");
createAccount.CoffePoints = int.Parse(Console.ReadLine());
List<Client> accountData = new List<Client>()
{
createAccount
};
当您明白为什么这样做时,我建议您按照 Isma 的建议去做(如果您已经学会了如何制作自己的方法),以使您的代码更清晰。我写这篇文章是为了帮助您了解为什么您写的内容没有效果。 Shorthand 属性 像这样的初始值设定项:
Something s = new Something(){
Property1 = ReadLine(), //no semicolon here, this is all
Property2 = ReadLine() //one line of code in a=1,b=2,c=3 pattern
};
不能有这样的多行代码:
Something s = new Something(){
Property1 = WriteLine("Blah"); ReadLine(); //can't put a semicolon here
Property2 = WriteLine("Blah"); ReadLine(); //it HAS to be a comma, because it
Property3 = WriteLine("Blah"); ReadLine(); //HAS to be a single line of code
};
请记住,在 C# 中定义新代码行的不是 return 键,而是分号。这只是一种语言规则,这样设置属性的模式是单行的,并且只能在 =
的右侧出现一条语句
您必须要么不使用 shorthand 方式(如上),要么您必须将要使用的所有多行代码放入一个方法中,然后调用该方法(如 Isma 所建议的)
我还想指出,您说您想要 "writeline a message after every readline" - 请注意,您的程序将等待用户输入任何内容,然后才会打印您的消息。 Isma 的方式(以及上面的方式)在请求 readline 之前打印一条消息,因为这通常是您想要做的。
如果你真的想要它之后,然后将它们移动到之后(但我想你真的只能感谢他们的投入,过度感谢的事情很烦人......)所以像这样的东西(Isma's方式):
private static string ReadLine(string writeMessage)
{
string s = Console.ReadLine();
Console.WriteLine(writeMessage);
return s;
}
或者我的方式:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Thanks for entering NameOfUser..");
在这段代码中,我希望在每个 Console.ReadLine() 之后添加一个 Console.Writeline() 但是我如何实现代码却给我一个错误。还有另一种方法可以在实例中添加 Console.WriteLine() 吗?
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create")
{
NameOfUser = Console.ReadLine(),
SurnameOfUser = Console.ReadLine(),
UserID = Console.ReadLine(),
UserEmail = Console.ReadLine(),
UserHomeAdd = Console.ReadLine(),
UserMobileNumber = int.Parse(Console.ReadLine()),
UsernameField = Console.ReadLine(),
PasswordField = Console.ReadLine(),
CoffePoints = int.Parse(Console.ReadLine())
};
List<Client> accountData = new List<Client>()
{
createAccount
};
您可以创建一个方法来打印某些内容和 returns Console.ReadLine(),例如:
private static string ReadLine(string writeMessage, bool parseAsInt = false)
{
Console.WriteLine(writeMessage);
var line = Console.ReadLine();
if (parseAsInt)
{
int parseInt = 0;
int.TryParse(line, out parseInt);
line = parseInt.ToString();
}
return line;
}
那么在创建对象的时候调用即可:
Client createAccount = new Client("Create")
{
NameOfUser = ReadLine("What's your name?"),
SurnameOfUser = ReadLine("Input your surname"),
[...]
CoffePoints = ReadLine("Coffe points?", true)
};
您不能将 WriteLine() 放在 ReadLine() 之间,因为您正在初始化新客户端的属性。但是,您可以这样做:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
Console.WriteLine("Enter NameOfUser ");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Enter SurnameOfUser ");
createAccount.SurnameOfUser = Console.ReadLine();
Console.WriteLine("Enter UserID ");
createAccount.UserID = Console.ReadLine();
Console.WriteLine("Enter UserEmail ");
createAccount.UserEmail = Console.ReadLine();
Console.WriteLine("Enter UserHomeAdd ");
createAccount.UserHomeAdd = Console.ReadLine();
Console.WriteLine("Enter UserMobileNumber ");
createAccount.UserMobileNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Enter UsernameField ");
createAccount.UsernameField = Console.ReadLine();
Console.WriteLine("Enter PasswordField ");
createAccount.PasswordField = Console.ReadLine();
Console.WriteLine("Enter CoffePoints ");
createAccount.CoffePoints = int.Parse(Console.ReadLine());
List<Client> accountData = new List<Client>()
{
createAccount
};
当您明白为什么这样做时,我建议您按照 Isma 的建议去做(如果您已经学会了如何制作自己的方法),以使您的代码更清晰。我写这篇文章是为了帮助您了解为什么您写的内容没有效果。 Shorthand 属性 像这样的初始值设定项:
Something s = new Something(){
Property1 = ReadLine(), //no semicolon here, this is all
Property2 = ReadLine() //one line of code in a=1,b=2,c=3 pattern
};
不能有这样的多行代码:
Something s = new Something(){
Property1 = WriteLine("Blah"); ReadLine(); //can't put a semicolon here
Property2 = WriteLine("Blah"); ReadLine(); //it HAS to be a comma, because it
Property3 = WriteLine("Blah"); ReadLine(); //HAS to be a single line of code
};
请记住,在 C# 中定义新代码行的不是 return 键,而是分号。这只是一种语言规则,这样设置属性的模式是单行的,并且只能在 =
您必须要么不使用 shorthand 方式(如上),要么您必须将要使用的所有多行代码放入一个方法中,然后调用该方法(如 Isma 所建议的)
我还想指出,您说您想要 "writeline a message after every readline" - 请注意,您的程序将等待用户输入任何内容,然后才会打印您的消息。 Isma 的方式(以及上面的方式)在请求 readline 之前打印一条消息,因为这通常是您想要做的。
如果你真的想要它之后,然后将它们移动到之后(但我想你真的只能感谢他们的投入,过度感谢的事情很烦人......)所以像这样的东西(Isma's方式):
private static string ReadLine(string writeMessage)
{
string s = Console.ReadLine();
Console.WriteLine(writeMessage);
return s;
}
或者我的方式:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Thanks for entering NameOfUser..");