C#中的Console.WriteLine()怎么用

c#
小億
171
2024-01-22 17:56:07
欄目: 編程語言

Console.WriteLine()是C#中用于打印輸出的方法。它可以接受一個(gè)或多個(gè)參數(shù),并將它們輸出到控制臺(tái)窗口。

下面是一些使用Console.WriteLine()方法的例子:

  1. 輸出字符串類型的內(nèi)容:
string name = "John";
Console.WriteLine("My name is " + name);
  1. 輸出數(shù)字類型的內(nèi)容:
int age = 25;
Console.WriteLine("I am " + age + " years old");
  1. 輸出多個(gè)參數(shù):
string firstName = "John";
string lastName = "Doe";
Console.WriteLine("My name is {0} {1}", firstName, lastName);

或者使用字符串插值(String Interpolation):

string firstName = "John";
string lastName = "Doe";
Console.WriteLine($"My name is {firstName} {lastName}");
  1. 輸出格式化字符串:
int number = 42;
Console.WriteLine("The answer to the ultimate question of life, the universe, and everything is {0:D}", number);

上述例子中的Console.WriteLine()方法會(huì)將參數(shù)中的內(nèi)容輸出到控制臺(tái)窗口,并自動(dòng)換行。

0