[英]working with console writeline c#
我需要使用相同的Console.WriteLine
輸出d
的值。 但是我只得到Result而不是輸出中d
的值。 我可以通過什么方式實現這一目標?
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));
}
}
}
使用:
Console.WriteLine("Result {0}", d);
您正在使用此重載 。
更新
如果您查看上面的鏈接,則可以閱讀其工作原理。 簡而言之,首先指定格式,其中{0}引用param對象數組的第一個值,{1}引用param對象數組的第二個值,依此類推。格式化之后,您要指定要使用的對象。
因此,在您的情況下,您需要一個值,這意味着兩個參數,一個格式和一個值。 因此,帶有d的“結果{0}”將變為(例如,當d = 10時)“結果10”。
注意:也刪除了不必要的括號。
使用
Console.WriteLine("Result {0}", d);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.