如何将空格添加到我的代码输出中?

huangapple go评论71阅读模式
英文:

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

  1. 将它们连接起来,就像您处理其他字符串一样:

    Console.WriteLine("欢迎 " + firstName + " " + midInitial + " " + lastName)

  2. 您可以使用复合格式化,在字符串中放置变量值的占位符,然后跟随字符串的变量名称:

    Console.WriteLine($"欢迎 {0} {1} {2}", firstName, midInitial, lastName);

  3. 您可以使用插值字符串,在字符串前面加上 $,将变量名放在字符串中,用大括号括起来:

    Console.WriteLine($"欢迎 {firstName} {midInitial} {lastName}");

有关连接字符串的所有方法的更多信息,请参见:如何连接多个字符串(C#指南)

英文:

There are many ways to do it, the most common are:

  1. Concatenate them as you're doing the rest of the strings:

    Console.WriteLine("Welcome " + firstName + " " + midInitial + " " + lastName)

  2. 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);

  3. 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)

huangapple
  • 本文由 发表于 2023年3月31日 23:13:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定