Console.Writeline - 为什么缺少输出

时间:2015-01-04 11:20:49

标签: c#

C#的绝对新手。试图运行这个程序,输出根本不会显示任何计算。为什么?我不想通过p,q,r,s进行加,子,乘,除等等。另外,我怎样才能在&#34之间放置空格;请输入一个数字"和userName?

string userName;
double  x, y;

Console.WriteLine(" Enter Your Name ");
userName = Console.ReadLine();
Console.WriteLine(" Please Enter A Number "+ userName);
First = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter Another Number"+ userName);
Second = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The addition of Two Numbers is",x,y, x*y);
Console.WriteLine("The substraction of Two Numbers is",x,y,x/y);
Console.WriteLine("The multiplication of Two Numbers is",x,y,x * y);
Console.WriteLine("The division of Two Numbers given is", x,y,x / y);
Console.ReadKey();

2 个答案:

答案 0 :(得分:11)

当您传递其他参数以显示输出时,您必须通过在格式行中添加占位符来告诉WriteLine 以放置它,如下所示:

Console.WriteLine("The product of Two Numbers {0} and {1} is {2}", x, y, x*y);

职位从零开始。第一个附加参数(即x)的打印值将替换{0}; y的值将替换{1}x*y的值将替换最终输出中的{2}

您不必对userName执行此操作的原因是您传递了一个参数:

Console.WriteLine("Please Enter Another Number " + userName);

userName附加到"Please Enter Another Number"字符串的结果作为单个参数传递给WriteLine。您可以使用格式说明符重写它,如下所示:

Console.WriteLine("Please Enter Another Number {0}", userName);

答案 1 :(得分:0)

完全同意dasblinkenlight。此外,您可能会遇到这行代码

Console.WriteLine("{0}, {1}", "Hello", 53);

写入此行的结果:Hello,53。{0}是格式字符串后面的第一个参数的占位符,{1}是第二个,依此类推。这在.NET中称为复合格式 - http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.110%29.aspx