[英]working with console writeline c#
I need to output the value of d
using the same Console.WriteLine
. 我需要使用相同的Console.WriteLine
输出d
的值。 But i am only getting Result not the value of d
in output. 但是我只得到Result而不是输出中d
的值。 In what way i can achieve this? 我可以通过什么方式实现这一目标?
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
Console.WriteLine("Please Enter the first Digit");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter the Second Digit");
b = Convert.ToInt32(Console.ReadLine());
int d = a + b;
Console.WriteLine("Result",(d));
}
}
}
Use: 使用:
Console.WriteLine("Result {0}", d);
You are using this overload . 您正在使用此重载 。
UPDATE 更新
If you look at the link above, you can read how it works. 如果您查看上面的链接,则可以阅读其工作原理。 In short, first you specify the formatting, where {0} references the first value of the param object-array, {1} references the second value of the param object-array, etc. After the format, you give the objects to use. 简而言之,首先指定格式,其中{0}引用param对象数组的第一个值,{1}引用param对象数组的第二个值,依此类推。格式化之后,您要指定要使用的对象。
So in your case, you need a single value, which means two arguments, a format, and a value. 因此,在您的情况下,您需要一个值,这意味着两个参数,一个格式和一个值。 Hence "Result {0}" with d, which will become (when for example d=10) "Result 10". 因此,带有d的“结果{0}”将变为(例如,当d = 10时)“结果10”。
Note: also removed the unnecessary parenthesis. 注意:也删除了不必要的括号。
使用
Console.WriteLine("Result {0}", d);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.