我问这个问题的原因是因为第一个数字显示在控制台中,但第二个数字没有显示,它只是说{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}.
更多推荐
发布评论