Console.Writeline -- 为什么缺少输出
本文关键字:输出 为什么 Writeline Console | 更新日期: 2023-09-27 18:36:02
绝对是C#的新手。正在尝试运行该程序,但输出根本不会显示任何计算。为什么?我不想通过 p,q,r,s 进行加法、子法、乘法、除法等,另外,如何在"请输入数字"和用户名之间放置空格?
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();
当您传递其他参数以显示输出时,您必须通过在格式行中添加占位符来告诉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);
完全同意dasblinkenlight的观点。此外,您可能会遇到这行代码
Console.WriteLine("{0}, {1}", "Hello", 53);
结果写下了这一行:你好,53。{0} 是格式字符串后第一个参数的占位符,{1}是第二个参数,依此类推。这在 .NET 中称为复合格式 - http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.110%29.aspx