是否可以使用占位符字符串两次?(Is it possible to use the placeholder string twice?)

系统教程 行业动态 更新时间:2024-06-14 16:52:52
是否可以使用占位符字符串两次?(Is it possible to use the placeholder string twice?) namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console.WriteLine("Type a number"); ConsoleKeyInfo KeyInfo1 = Console.ReadKey(); Console.WriteLine("Type another number"); ConsoleKeyInfo KeyInfo2 = Console.ReadKey(); Console.WriteLine("The number is {0}", KeyInfo1.KeyChar.ToString() + "The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo2.KeyChar.ToString()); Console.ReadKey(); } } }

我问这个问题的原因是因为第一个数字显示在控制台中,但第二个数字没有显示,它只是说{1}。 我希望这是有道理的,我是编程的新手。

namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console.WriteLine("Type a number"); ConsoleKeyInfo KeyInfo1 = Console.ReadKey(); Console.WriteLine("Type another number"); ConsoleKeyInfo KeyInfo2 = Console.ReadKey(); Console.WriteLine("The number is {0}", KeyInfo1.KeyChar.ToString() + "The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo2.KeyChar.ToString()); Console.ReadKey(); } } }

The reason I'm asking this question is because the first number shows up in the console but the second number doesn't show up, it just says {1}. I hope this makes sense, I'm new to programming.

最满意答案

WriteLine函数的第一个参数是格式字符串。 这是你添加占位符的部分。 在第一个格式参数之后添加值以将占位符替换为参数,如下所示:

Console.WriteLine("The number is {0} The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo1.KeyChar.ToString(), KeyInfo2.KeyChar.ToString());

其中第一个参数(格式化后,即传递给函数的第二个参数)将替换{0} ,第二个参数将替换{1} 。

The first parameter of the WriteLine function is the format string. Thats the part where you add placeholders. You add the values to replace the placeholders as parameter after the first format parameter, like this:

Console.WriteLine("The number is {0} The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo1.KeyChar.ToString(), KeyInfo2.KeyChar.ToString());

Where the first parameter (after format, ie, second paramater passed into the function) will replace {0} and the second will replace {1}.

更多推荐

本文发布于:2023-04-05 12:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cebe8f081130d9591eae2b885f5b87a5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   可以使用   字符串   string   placeholder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!