如何在C#中從控制檯讀取一行內容?
在C#中,ReadLine()方法用於從控制檯讀取一行。
str = Console.ReadLine();
以上操作會將這一行內容設定為變數str。
示例
using System; using System.Collections.Generic; class Demo { static void Main() { string str; // use ReadLine() to read the entered line str = Console.ReadLine(); // display the line Console.WriteLine("Input = {0}", str); } }
輸出內容
Input =
上面我們使用Console.ReadLine()方法展示了一行內容。字串由使用者透過命令列輸入。
廣告