英文:
How do I add spaces to the output of my code?
问题
Console.WriteLine("Hello, What is your first name?");
String firstName = Console.ReadLine();
Console.WriteLine("What is your middle Initial?");
String midInitial = Console.ReadLine();
Console.WriteLine("What is your last name");
String lastName = Console.ReadLine();
Console.WriteLine("Welcome, " + firstName + " " + midInitial + " " + lastName);
Console.ReadLine();
英文:
Console.WriteLine("Hello, What is your first name?");
String firstName = Console.ReadLine();
Console.WriteLine("What is your middle Initial?");
String midInitial = Console.ReadLine();
Console.WriteLine("What is your last name");
String lastName = Console.ReadLine();
Console.WriteLine("Welcome "+ firstName + midInitial + lastName);
Console.ReadLine();
I want the output to be in the format "Welcome, Mark R Golding"
, however, I end up with "Welcome, MarkRGolding"
. How do I fix this?
I tried adding double quotes like so:
Console.WriteLine("Welcome, " + " " firstName + " " midInitial + " " lastName);
but that just gives me errors...
答案1
得分: 2
-
将它们连接起来,就像您处理其他字符串一样:
Console.WriteLine("欢迎 " + firstName + " " + midInitial + " " + lastName)
-
您可以使用复合格式化,在字符串中放置变量值的占位符,然后跟随字符串的变量名称:
Console.WriteLine($"欢迎 {0} {1} {2}", firstName, midInitial, lastName);
-
您可以使用插值字符串,在字符串前面加上
$
,将变量名放在字符串中,用大括号括起来:Console.WriteLine($"欢迎 {firstName} {midInitial} {lastName}");
有关连接字符串的所有方法的更多信息,请参见:如何连接多个字符串(C#指南)
英文:
There are many ways to do it, the most common are:
-
Concatenate them as you're doing the rest of the strings:
Console.WriteLine("Welcome " + firstName + " " + midInitial + " " + lastName)
-
You can use composite formatting by putting placeholders in the string for the variable values and follow the string with the variable names:
Console.WriteLine($"Welcome {0} {1} {2}", firstName, midInitial, lastName);
-
You can use an interpolated string by prefixing the string with a
$
and putting the variable names inside the string, surrounded by curly braces:Console.WriteLine($"Welcome {firstName} {midInitial} {lastName}");
For more information on all the ways to concatenate strings, see: How To Concatenate Multiple Strings (C# Guide)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论